Hi everyone... For almost all of my objects I put a series of classic REST admin links in views. For example, for a ''user'', I have <%= link_to "Show", user_path(user) %> <%= link_to "Edit", edit_user_path(user) %> <%= link_to "Destroy", user_path(user), :confirm => "Are you sure", :method => :delete %> 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? An idea anyone? In advance, thank you very much! Yannis -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Yannis, You could define a helper method as follows: def admin_links(user) "<%= link_to "Show", user_path(#{user}) %> <%= link_to "Edit", edit_user_path(#{user}) %> <%= link_to "Destroy", user_path(#{user}), :confirm => "Are you sure", :method => :delete %>" end 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. Regards Robin On Feb 29, 10:47 am, Yannis Jaquet <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi everyone... > > For almost all of my objects I put a series of classic REST admin links > in views. For example, for a ''user'', I have > > <%= link_to "Show", user_path(user) %> > <%= link_to "Edit", edit_user_path(user) %> > <%= link_to "Destroy", user_path(user), :confirm => "Are you sure", > :method => :delete %> > > 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? > > An idea anyone? > > In advance, thank you very much! > > Yannis > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yannis Jaquet wrote:> 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?The following works (for me) in a simple test: def admin_links element text = "<td> "+link_to(''Show'', element)+" </td>\n" text += "<td> "+link_to(''Edit'', self.send("edit_#{element.class.to_s.downcase}_path", element))+" </td>\n" text += "<td> "+link_to(''Destroy'', element, :confirm => ''Are you sure?'', :method => :delete)+" </td>\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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robin Fisher wrote:> Hi Yannis, > > You could define a helper method as follows: > > def admin_links(user) > "<%= link_to "Show", user_path(#{user}) %> > <%= link_to "Edit", edit_user_path(#{user}) %> > <%= link_to "Destroy", user_path(#{user}), :confirm => "Are you > sure", :method => :delete %>" > end > > 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. > > Regards > > Robin > > On Feb 29, 10:47�am, Yannis Jaquet <rails-mailing-l...@andreas-s.net>@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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---