Tim Kretschmer
Tim Kretschmer

Reputation: 2280

Helper for edit/show/destroy Link with an image

i am trying to make nice helpers so that i can use these style:

edit(category)
destroy(post.comment.first)
show(@user)

and we get the selected link with a nice image.

can anyone tell me if i am doing it right or is there a better magical rails way to get the url?

def show(object)
  link_to image_tag("admin/show.png"), eval("admin_{object.class.to_s.downcase}_path(#    {object.id})")
end

def edit(object)    
  link_to image_tag("admin/edit.png"), eval("edit_admin_#{object.class.to_s.downcase}_path(#{object.id})")
end

def destroy(object)    
  link_to image_tag("admin/destroy.png"), eval("admin_#{object.class.to_s.downcase}_path(#{object.id})"), :method=>:delete, :confirm=>"Do you really want to delete this?"
end

this is working fine but i am looking for the magic rails way :-)

Upvotes: 1

Views: 531

Answers (1)

ShiningRay
ShiningRay

Reputation: 1008

replace eval with send, and replace downcase with underscore which is rails' convention

send("admin_{object.class.to_s.underscore}_path", object.id)

BTW, rails can do these for you:

# equals to your `show(object)`
link_to image_tag('admin/show.png'), [:admin, object]

Upvotes: 1

Related Questions