# Resource\_feeder and nested resources

**URL:** <https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625>\
**Category:** Rails\
**Created:** [March 30, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625 "2007-03-30T13:55:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [March 30, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625/1 "2007-03-30T13:55:13Z")

</div>

hi,  
I’ve a problem with resource\_feeder plugin, basically works fine until  
we give nested resource url.

Basically: /blog/1/posts/2, named route: blog\_post\_url

This needs :blog\_id and :id passed as a parameter, otherwise it cannot  
work.

I’ve managed to go through some :link =\> lamda { |o| whatever… }  
in :feed and :item options, but I’ve got no luck.  
Also I’ve tried to specify :url\_rewriter option, but I come up with  
nothing.

any help would be appreciated, thanks.

[…]  
@posts = BlogPost.find(:all, :order =\> ‘created\_at DESC’, :limit  
=\> limit)  
options\_for\_feed = {  
:feed =\> { :title =\> “Latest #{limit} Blog Posts”},  
:item =\> { :description =\> :excerpt }  
}  
respond\_to do |format|  
format.html  
format.rss { render\_rss\_feed\_for(@posts, options\_for\_feed) }  
format.atom { render\_atom\_feed\_for(@posts, options\_for\_feed) }  
end

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [March 30, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625/2 "2007-03-30T18:46:21Z")

</div>

Hi Claudio,

This _may_ work, it’s untested, completely off the top of my head,  
_and_ it will only work to generate urls for records with existing  
ids (which your blog\_posts do have).

In the lib directory create a file called blog\_post\_url\_writer.rb  
with the following contents:

class BlogPostUrlWriter  
def initialize(controller)  
@controller = controller  
end

def blog\_post\_url(blog\_post)  
@controller.send :blog\_post\_url, blog\_post.blog\_id, blog\_post  
end  
end

Then, in your controller you do:

blog\_post\_url\_writer = BlogPostUrlWriter.new(self)

and in your options\_for\_feed you add:

:url\_writer =\> blog\_post\_url\_writer

That _should_ route the calls resource feeder makes to blog\_post\_url  
through your special BlogPostUrlWriter instance, which as you can see  
above, inserts the :blog\_id into the call to the _real_ blog\_post\_url  
helper.

HTH,  
Trevor

On 30-Mar-07, at 4:54 AM, Claudio P. ð wrote:

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [March 30, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625/3 "2007-03-30T19:04:24Z")

</div>

hi Trevor,  
thanks so much for this code, well written and I will test it soon as  
I can.

thanks again.

Claudio

Il giorno 30/mar/07, alle ore 18:43, Trevor S. ha scritto:

---

<div class="post-metadata">

**Author:** ![crossblaim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7feea3/32.png) [@crossblaim](https://www.ruby-forum.com/u/crossblaim)\
**Post date:** [March 30, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/resource-feeder-and-nested-resources/90625/4 "2007-03-30T19:23:30Z")

</div>

On Mar 30, 6:43 pm, Trevor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Claudio,
> 
> This _may_ work, it’s untested, completely off the top of my head,  
> _and_ it will only work to generate urls for records with existing  
> ids (which your blog\_posts do have).  
> […]

allright, now I’m not in hurry and I can write a complete response 🙂

your code works wonderfully, and I’ll blog about it right now (  
[http://railswatcher.com](http://railswatcher.com) ) giving you credit.

For people that doesn’t want to bother reading the article here’s the  
code for controller:

def index  
limit = 10  
@posts = BlogPost.find(:all, :order =\> ‘created\_at DESC’, :limit  
=\> limit)  
blog\_post\_url\_writer = BlogPostUrlWriter.new(self)  
options\_for\_feed = {  
:feed =\> { :title =\> “Latest #{limit} Blog Posts on  
[iCoreTech.org](http://iCoreTech.org)”, :link =\> “[http://example.com](http://example.com)” },

# remember to fill those fields, title, description and pub\_date, I’ve

encountered problems without.  
:item =\> { :title =\> :title, :description =\> :excerpt, :pub\_date  
=\> :created\_at },  
:url\_writer =\> blog\_post\_url\_writer  
}  
respond\_to do |format|  
format.html  
format.rss { render\_rss\_feed\_for(@posts, options\_for\_feed) }  
format.atom { render\_atom\_feed\_for(@posts, options\_for\_feed) }  
end  
end

Thanks!

Claudio
