Restful routes in a polymorphic association

Hello,
I have an Entry model with polymorphic associations with all kinds of
contents, like notes, addresses, etc.

Class Entry < ActiveRecord::Base
belongs_to :content, :polymorphic => true
end

Class Note < ActiveRecord::Base
has_one :entry, :as => :content
end

Class Address < ActiveRecord::Base
has_one :entry, :as => :content
end

Now, I am trying to follow the RESTful path, so in routes.rb I have:
map.resources :entries, :notes, :addresses, …

I have an index action in entries_controller that retrieves all
@entries and, finally, index.rhtml has the following:

<% for entry in @entries %> <% end %>
Type Title
<%= entry.content_type %> <%= link_to entry.content.title, entry_path(entry) %>

BUT–here’s the thing. I DON’T really want entry_path to generate the
linked URL. Rather, I would very much like each individual entry’s
content type to determine routing. Thus, for example, if
entry.content_type is “Note”, then I would like notes_controller
actions to be invoked, as if I had written the following:
<%= link_to entry.content.title, note_path(entry.content_id) %>

Does this make sense? At the very least I am sure the question is
going to give away my poor knowledge of Ruby…

Many thanks in advance,
Giuseppe B.

…I mean, of course I could simply do:

<%= link_to entry.content.title, "/notes/#{entry.content_id} %>

but I’d rather not, of course…

I guess my question may boil down to the following.
Is it possible in Ruby to place a method call by putting together a
string at runtime and having it executed?
In this particular case the string might look like:
s = “#{entry.content_type}_path(#{entry.content_id})”

Thanks again in advance
Giuseppe

Giuseppe írta:

s = “#{entry.content_type}_path(#{entry.content_id})”

sure:
s = eval("#{entry.content_type}_path(#{entry.content_id})")

Thanks again in advance
Giuseppe


Parragh S.
e-mail: [email protected]
web: parszab.nir.hu

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs