I am tired of writing those links down over and over and look for a way
to DRY it a little bit.
I am thinking about a helper method allowing me to write a simple
<%= admin_links(user) %>
but I don’t know how to dynamically write the user_path, edit_user_path
and so on?
In your controller, you would probably define the @user(s) variable
containing either a single user or an array of users. So, in your
view, you would call:
<%= admin_links(@user) %>
or if iterating through an array of users:
<%= admin_links(user) %>
This is untested and I think the helper method might need a little
refining but it shows how to pass the user variable through to
generate the correct links.
I am tired of writing those links down over and over and look for a way
to DRY it a little bit.
I am thinking about a helper method allowing me to write a simple
<%= admin_links(user) %>
but I don’t know how to dynamically write the user_path, edit_user_path
and so on?
"+link_to('Destroy', element, :confirm => 'Are you
sure?', :method => :delete)+"
\n"
text
end
There may be plugins or gems that already do this, but I haven’t had a
look…
I don’t really like the way I have constructed the edit_XXX_path method
name, in particular because it won’t work for named routes, but it works
for simple standard routes and is a start…
In your controller, you would probably define the @user(s) variable
containing either a single user or an array of users. So, in your
view, you would call:
<%= admin_links(@user) %>
or if iterating through an array of users:
<%= admin_links(user) %>
This is untested and I think the helper method might need a little
refining but it shows how to pass the user variable through to
generate the correct links.
@Robin
Thank you for your answer.
I think my question was not really clear, sorry for that.
In fact I would like to have my admin_links(object) method to accept any
object having resources.
Then I would be able to admin_links(object) and have the links returned
as ‘object’path(object) or edit’object’_path(object).
@Mark
Again, thank you very, much that’s exactly what I was looking for…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.