Note that there are some explanatory texts on larger screens.

plurals
  1. POrenaming routes (map, link_to, to_param) in rails
    text
    copied!<p>I'm having a little issue...I setup a rails application that is to serve a german website. To make use of Rails' internal pluralization features, I kept all my models in english (e.g. the model "<code>JobDescription</code>"). Now, if I call "<code>http://mysite.com/job_descriptions/</code>", I get all my <code>job_descriptions</code>....so far, so good. Because I didn't want the english term "<code>job_descriptions</code>" in my url, I put the following into my routes.rb</p> <pre><code>map.german_term '/german_term', :controller =&gt; 'job_descriptions', :action =&gt; 'index' map.german_term '/german_term/:id', :controller =&gt; 'job_descriptions', :action =&gt; 'show' </code></pre> <p>If I call "<code>http://mysite.com/german_term/</code>" or "<code>http://mysite.com/german_term/283</code>" I get all my <code>job_descriptions</code>, which is fine.</p> <p>However, to make the URL more SEO friendly, I'd like to exchange the id for a more userfriendly slug in the URL. Thus, I put the following in my <code>job_description.rb</code>:</p> <pre><code>def to_param "#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}" end </code></pre> <p>which, whenever I use "<code>job_description_path</code>" in any <code>link_to</code> method, renders my URLs out to something like "<a href="http://mysite/job_descriptions/13-my-job-description-title" rel="nofollow noreferrer">http://mysite/job_descriptions/13-my-job-description-title</a>".</p> <p>However, and this is where I'm stuck, I'd like to get "<code>http://mysite/german_term/13-my-job-description-title</code>". I already tried to exchange the "<code>job_description_path</code>" with "<code>german_term_path</code>" in the link_to code, but that only generates "<code>http://mysite/german_term/13</code>". Obviously, <code>to_param</code> isn't called. One workaround I found is to build the link with:</p> <pre><code>&lt;%= link_to job_description.name, german_term_path(job_description.to_param) %&gt; </code></pre> <p>But that's rather tedious to change all the <code>link_to</code> calls in my code. What I want is to replace "<code>job_description</code>" by "<code>german_term</code>" whenever it occurs in a URL.</p> <p>Any thoughts?!?</p> <p>Regards, </p> <p>Sebastian</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