Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with "link_to"
    text
    copied!<p>NEW EDITING</p> <p>With <code>resources :cases</code> in the <code>routes.rb</code> all these routing helpers work as expected (c is an instance of the model Case):</p> <pre><code>cases_path # Index action new_case_path # New action case_path(c) # Show action edit_case_path(c) # Edit action </code></pre> <p>Changing <code>routes.rb</code> to this:</p> <pre><code>scope "/:area" do resources :cases end </code></pre> <p>two routing helpers fail (see error below):</p> <pre><code>cases_path # OK new_case_path # OK case_path(c) # Fail edit_case_path(c) # Fail </code></pre> <p>To make it works I need to change the last two helpers in:</p> <pre><code>cases_path # OK new_case_path # OK case_path(c, :area =&gt; params[:area]) # OK edit_case_path(c, :area =&gt; params[:area]) # OK </code></pre> <p>Why there is no need to give :area to the first two helpers and there is a need for the second two? It makes the code confusing...</p> <p>OLD QUESTION</p> <p>Initially I had this routes.rb:</p> <pre><code>resources :cases </code></pre> <p>and I could generate links of this type</p> <pre><code>example.com/cases/3 </code></pre> <p>to show the case with ID = 3 with:</p> <pre><code>link_to("Show this case", @case) </code></pre> <p>Then I modified routes.rb to:</p> <pre><code>scope "/:area" do resources :cases end </code></pre> <p>end the link_to above give me the error</p> <pre><code>No route matches {:action=&gt;"show", :controller=&gt;"cases", :area=&gt;#&lt;Case id: 2, </code></pre> <p>It seems that rails does not get the :area from the parameters automatically. I could fix this forcing the area creating a link with</p> <pre><code>link_to("Show this case", case_path(params[:area], @item)) </code></pre> <p>to obtain the link to</p> <pre><code>example.com/area1/cases/3 </code></pre> <p>but I really don't like it. Am I doing something wrong? Why rails bring over the controller and the action but forget about the area?</p>
 

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