Forum: Ruby on Rails resource_feeder and nested resources

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Claudio P. (Guest)
on 2007-03-30 15:55
(Received via mailing list)
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
Trevor S. (Guest)
on 2007-03-30 20:46
(Received via mailing list)
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:
Claudio P. (Guest)
on 2007-03-30 21:04
(Received via mailing list)
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:
Claudio P. (Guest)
on 2007-03-30 21:23
(Received via mailing list)
On Mar 30, 6:43 pm, Trevor S. <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 ) 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", :link => "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
This topic is locked and can not be replied to.