Dynamic Paths in Helper


Dynamic Paths in Helper



I'm trying to create a helper method for my admin links. In quite a few views I have the code

<% if current_user %> <%= link_to "Edit", edit_model_path(model) %> <%= link_to "New", new_model_path %> <%= link_to "Delete", model, :confirm => "You're a Noob",  :method => :delete %> <% end %> 

that only display these when logged in.

I would like to do something like this in their place

<%= admin_links(model) %> 

and pass the current item into the application helper method

def admin_links(m)  if current_user   a = "#{link_to "edit" edit_m_path(m)}"   a << "#{link_to "new" new_m_path}"   a << "#{link_to "Delete", m, :confirm => "Your a Noob",  :method => :delete}"  end end 

Or something of the like.




How can you send a file to S3 after all processing is done using paperclip in rails?

1:



Returning from rails controller
basically you need to transform the class name of the model into something pointing to the correct path..
Is this RESTful?
model_name = m.class.to_s.underscore 
And then use it to call the appropriate path methods.
Installing SQLite3 for Ruby on Windows - what's the current easiest route?
link_to "edit", send("edit_#{model_name}_path", m) 
As an aside, you don't need to put the link_tos in #{} because that function simply returns a string..
Ruby and Rails - oauth and http proxy


Architectural best practices - where to put non-REST actions?


rendering open office files in ruby on rails

2:



How to best secure records in Rails?
Rails provides polymorphic routes to handle this problem: http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html.
= link_to "Edit", polymorphic_path(model), :method => :put 


3:


I would use a partial for this - instead of a helper.

Wherever you want to display these links in your views, simply render the partial:.
<%= render :partial => "admin_links", :locals => { :model => model } %> 
In _admin_links.html.erb just paste the original code:.
<% if current_user %>   <%= link_to "Edit", edit_model_path(model) %>   <%= link_to "New", new_model_path %>   <%= link_to "Delete", model, :confirm => "Your a Noob", :method => :delete %> <% end %> 



70 out of 100 based on 80 user ratings 880 reviews