Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>scope ":username" do resources :feedbacks end </code></pre> <p>From the <a href="http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers" rel="nofollow noreferrer">docs</a>:</p> <blockquote> <p>This will provide you with URLs such as /bob/posts/1 and will allow you to reference the username part of the path as params[:username] in controllers, helpers and views.</p> </blockquote> <hr> <p>UPDATE:</p> <p>I have tested and confirmed the accuracy of paozac's <a href="https://stackoverflow.com/questions/7297231/how-do-i-get-the-format-of-my-urls-to-be-username-controller-id-in-rails-3-1/7350998#7350998">answer</a>. I'll clarify it a bit.</p> <p>Suppose you had a <code>@feedback</code> object with an <code>id</code> of <code>12</code>, and the associated user had a <code>username</code> of <code>foouser</code>. If you wanted to generate a URL to the edit page for that <code>@feedback</code> object, you could do the following:</p> <pre><code>edit_feedback_url(:username =&gt; @feedback.user.username, :id =&gt; @feedback) </code></pre> <p>The output would be <code>"/foouser/feedbacks/12/edit"</code>.</p> <pre><code># A URL to the show action could be generated like so: feedback_url(:username =&gt; feedback.user.username, :id =&gt; feedback) #=&gt; "/foouser/feedbacks/12" # New new_feedback_url(:username =&gt; current_user.username) #=&gt; "/foouser/feedbacks/new" </code></pre> <p>Additionally, as noted by nathanvda in the comments, you can <a href="https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L181" rel="nofollow noreferrer">pass ordered arguments</a> which will be matched with the corresponding dynamic segment. In this case, the username must be passed first, and the feedback id should be passed second, i.e.:</p> <pre><code>edit_feedback_url(@feedback.user.username, @feedback) </code></pre> <p>Also, if you need help handling the params from the controller, I suggest creating a new question specific to that.</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