views:

29

answers:

2

Hi. I've got model with many associations.

User :has_many => :posts, :videos, :images etc

I've got helper method, which receives one of this association

draw_chart user.videos

def draw_chart(assoc)
  assoc.each do |a|
    link_to "update", [a.user, a], :method => :put, :remote => true, :confirm => 'orrly?'
  end
end

Problem here is to send extra parameters to update action.

For example I want to get this url: /users/1/videos/2?status=done but I am confused how to realize this while I am using [a.user, a] instead of user_videos_path(video, :status => 'done'). But I can't use last, because it can be either videos, or images or anything else

A: 

Only solution I can think up is this strange one:

link_to "update", eval("user_#{a.class.name.tableize}_path(#{a.school.id}, #{a.id}, :status => 'done')"), :method => :put, :remote => true, :confirm => 'orrly?'

but this looks messy

fl00r
+1  A: 

I wont go using eval, how about this

<%= link_to('update', self.send("user_#{a.class.name.pluralize}_path", a, , :status => 'done'), :method => :put, :remote => true, :confirm => 'orrly?')  %>
nas
this is good (but you should use tableize istead of pluralize, or underscore.pluralize). but here is another problem (as in my solution) link became not proper: `/users/1/videos.8?status=done`. This __dot__ is not something I am looking for :)
fl00r
Totally agree with tableize part but not at all sure about .8
nas
path is `self.send("user_#{a.class.name.tableize}_path", a.user, a , :status => 'done')` and result is with this strange dot.
fl00r