views:

99

answers:

1

I have the following partial. It can be called from three different times in a view as follows:

 <%= render :partial => "contact_event",
     :collection => @contacts,
        :locals => {:event => email} %>

Second time:

 <%= render :partial => "contact_event",
     :collection => @contacts,
        :locals => {:event => call} %>

Third time:

 <%= render :partial => "contact_event",
     :collection => @contacts,
        :locals => {:event => letter} %>

In each instance, call, email, letter refer to a specific instance of a Model Call, Email, or Letter.

Here is what I tried to do and conceptually what I'd like to do: assign the route based on the class name that has been passed to the :event from the :partial.

What I did was create what the actual url should be. The 'text' of it is correct, but doesn't seem to recognize it as a named route.

 <!-- provide a link to skip this item -->
 <% url = "skip_contact_#{event.class.name.tableize.singularize}_url" %>
 <%= link_to_remote "Skip #{url} Remote",
    :url => send("#{url}(contact_event, event)")
    :update => "update-area-#{contact_event.id}-#{event.id}" %>
 <span id='update-area-<%="#{contact_event.id}-#{event.id}"%>'> </span>

The result of the above: when event has been passed an email instance, for example, it says:

skip_contact_email_url not a method.

The url is right, but it doesn't recognize as a method.

  1. How can I dynamically define skip_contact_email_url to be skip_contact_letter_url if the local variable is letter?

  2. Even better, how can I have a single named route that would do the appropriate action?

+2  A: 

You can use polymorphic_url. It generates corresponding route based on item types:

Edit: The route is generated based on record's class, so if you pass :event => call or :event => email, it will work like this:

# event.class == Email
polymorphic_url([contact_event, event], :action => :skip)
#=> /contact_events/:contact_event_id/emails/:id/skip

# event.class == Call
polymorphic_url([contact_event, event], :action => :skip)
#=> /contact_events/:contact_event_id/calls/:id/skip

etc.

Edit2: Routes:

map.resources :contacts do |contact|
  contact.with_options :member => {:skip => : ... [get/post - what you have] } do |c|
    c.resources :letter
    c.resources :emails
    c.resources :calls
  end
end
Voyta
can I dynamically assign the value in event? that's what I want to do....
Angela
the value for event is coming through the :locals hash
Angela
Yes, you assign any ActiveRecord object as `:event`, and then use it. See edit.
Voyta
okay this is interesting -- it's spinning my head just a little bit, though...but I think this is the way to do it. How do I create the routes, though to make this work?
Angela
oh wait, so if I make emails, calls nested into a class called contact_events...is that the idea....? I currently don't really have a concept of "contact_event" -- I use that to store the instance of Contact...
Angela
See edit for routes. I assume you have classes `Contact`, `Email`, `Letter` and `Call` and controllers `EmailsController`, `LettersController`, `CallsController`.
Voyta