Forum: Ruby on Rails restful routes in a polymorphic association

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
6b71251b5261e1fe3142bf620e573724?d=identicon&s=25 Giuseppe (Guest)
on 2007-02-27 20:29
(Received via mailing list)
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

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

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

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 %>
<td><%= entry.content_type %></td>
<td><%= link_to entry.content.title, entry_path(entry) %></td>
<% end %>

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 Bertini
6b71251b5261e1fe3142bf620e573724?d=identicon&s=25 Giuseppe (Guest)
on 2007-02-27 21:24
(Received via mailing list)
...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
88e98c5398a3b4af4d0eaefacf69f8b7?d=identicon&s=25 Parragh Szabolcs (Guest)
on 2007-03-02 18:22
(Received via mailing list)
Giuseppe írta:
> s = "#{entry.content_type}_path(#{entry.content_id})"
s = eval("#{entry.content_type}_path(#{entry.content_id})")

> Thanks again in advance
> Giuseppe
> >

Parragh Szabolcs
This topic is locked and can not be replied to.