views:

194

answers:

2

I'm trying to use the Rails Atom Feed Helper to generate a feed for a nested resource. My view template (index.atom.builder) is:

atom_feed(:schema_date => @favourites.first.created_at) do |feed|
  feed.title("Favourites for #{@user.login}")

  feed.updated(@favourites.first.created_at)

  @favourites.each do |favourite|
    feed.entry(favourite, :url => favourite.asset.external_ref) do |entry|
      entry.title(favourite.asset.external_ref)
      entry.content(image_tag(favourite.asset.location), :type => 'html')
      entry.author do |author|
        author.name(@user.login)
      end
    end
  end
end

And I have the following routes:

  map.namespace :public do |pub|
    pub.resources :users, :has_many => [ :favourites ]
    pub.resources :favourites
    pub.resources :assets, :only => [ :show ]
  end

Unfortunately the url is failing to generate for the feed.entry line:

feed.entry(favourite, :url => favourite.asset.external_ref) do |entry|

The error is "undefined method `favourite_url' for ActionView::Base".

I've tried changing the feed.entry line to:

feed.entry([:public, favourite], :url => favourite.asset.external_ref) do |entry|

But this then returns the entry for an Array rather than a favourite! Someone had a similar problem here also.

I know that adding the line:

map.resource :favourites

to my routes.rb would 'fix' this problem but this resource is only available nested beneath the /public namespace.

Has anyone had this problem before?

Cheers Arfon

A: 

You are using favourite.asset.external_ref as the title of the entry, which leaves me to believe the URL for that entry should probably be defined as:

public_user_favourite_url(:id => favourite, :user_id => @user)

Which, if favorite.id = 9 and @user.id = 1, would generate:

http://localhost:3000/public/users/1/favourites/9

Is this what you are looking for?

Michael Sepcot
Hey thanks, that worked! I'm not sure how I missed that option :-)
arfon
A: 

Just to follow up. Based upon Michael's suggestion I'm passing the full url param and this seems to generate the correct url for the feed.entry line.

  @favourites.each do |favourite|
    feed.entry(favourite, :url => public_user_favourite_url(:id => favourite, :user_id => @user)) do |entry|
      entry.title(favourite.asset.external_ref)
      entry.content(image_tag(favourite.asset.location), :type => 'html')
      entry.author do |author|
        author.name(@user.zooniverse_user_id)
      end
    end
  end
arfon