Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I dynamically change the path_to()?
    text
    copied!<p>I currently have three methods which I want to collapse into one:</p> <pre><code> def send_email(contact,email) end def make_call(contact, call) return link_to "Call", new_contact_call_path(:contact =&gt; contact, :call =&gt; call, :status =&gt; 'called') end def make_letter(contact, letter) return link_to "Letter", new_contact_letter_path(:contact =&gt; contact, :letter =&gt; letter, :status =&gt; 'mailed') end </code></pre> <p>I want to collapse the three into one so that I can just pass the Model as one of the parameters and it will still correctly create the path_to. I am trying to do this with the following, but stuck:</p> <pre><code> def do_event(contact, call_or_email_or_letter) model_name = call_or_email_or_letter.class.name.tableize.singularize link_to "#{model_name.camelize}", new_contact_#{model_name}_path(contact, call_or_email_or_letter)" end </code></pre> <p>Thanks to the answers here, I have tried the following, which gets me closer:</p> <pre><code>link_to( "#{model_name.camelize}", send("new_contact_#{model_name}_path", :contact =&gt; contact, :status =&gt; "done", :model_name =&gt; model_name) ) </code></pre> <p>But I can't seem to figure out how to past the #{model_name} when it is an :attribute and then send the value of model_name, not as a string, but referring the object.</p> <p><strong>I got this to work:</strong> -- giving points to Kadada because he got me in the right direction :)</p> <pre><code> def do_event(contact, call_or_email_or_letter) model_name = call_or_email_or_letter.class.name.tableize.singularize link_to( "#{model_name.camelize}", send("new_contact_#{model_name}_path", :contact =&gt; contact, :status =&gt; 'done', :"#{model_name}" =&gt; call_or_email_or_letter ) ) end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload