Kourosh Kourosh - 1 year ago 89
Ruby Question

link_to tag used for two controllers with shared (one) view

I have two controllers (template and estimate) sharing one view action. in the index page i have the following code:

- if can? :create, Estimate
= link_to "New Estimate", eval("new_#{params[:controller].singularize}_path"), class: 'btn btn-primary btn-xs', data: ajax_modal_data("form")

I want the link_to "New Estimate" to be changed "New Templates" in the index page when the template controller is used.

Answer Source

Some notes: try avoiding #eval method in rails, and params variable outside of controller code, so:

= link_to "New #{env.camelize}", send(:"new_{env}_path"), class: 'btn btn-primary btn-xs', data: ajax_modal_data("form")

and call of controller to render:

 render :index, locals: { env: safe_controller_name }

 def safe_controller_name
    controllers = %w(templates estimates)
    if ! controllers.include? params[:controller]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download