title: Rails 3 / nested routes: Problem to redirect to the correct url for a collection of microposts in a user show page
body: <p>I have a problem with nested routes which don't display correct url. </p>
  
  <p>I use the below code, but it renders something like:</p>
  
  <pre><code>.../users/[:user_id]/microposts/[:user_id] 
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>.../users/[:user_id]/microposts/[:micropost_id] 
  </code></pre>
  
  <p>for every microposts displayed in the user show pages.</p>
  
  <h1>Associations</h1>
  
  <pre><code>has_many :microposts #user model
  belongs_to :users #micropost model
  </code></pre>
  
  <h1>routes</h1>
  
  <pre><code>resources :microposts, :only =&gt; [:create, :destroy]
  resources :users, :only =&gt; [:show] do
    resources :microposts, :only =&gt; [:show]  
  end
  </code></pre>
  
  <h1>Microposts_controller</h1>
  
  <pre><code>def show
    @user = User.find(params[:user_id])
    @micropost = @user.microposts.find(params[:id])
  end
  </code></pre>
  
  <h1>microposts/_micropost.html.erb</h1>
  
  <pre><code>&lt;%= link_to "show this micropost" user_micropost_url(@user) %&gt; #link for every microposts displayed in the user show pages
  </code></pre>
  
  <h1>user/show.html.erb</h1>
  
  <pre><code>&lt;%= render @microposts %&gt;
  </code></pre>
  
  <p>I can't see where I am wrong.
  Thank a lot for your help!</p>
  
  <h1>EDIT</h1>
  
  <pre><code>if I use [@user, @micropost ] the displayed url is .../users/[:user_id]
  if I use user_micropost_url(@user, @micropost) it renders an error:
  
  No route matches {:action=&gt;"show", :controller=&gt;"microposts", :user_id=&gt;#&lt;User id: 1
  </code></pre>
  
  <p>Routes:</p>
  
  <pre><code>user_micropost GET /users/:user_id/microposts/:id(.:format) {:action=&gt;"show", :controller=&gt;"microposts"}
  microposts POST   /microposts(.:format) {:action=&gt;"create", :controller=&gt;"microposts"}
  micropost DELETE /microposts/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"microposts"}
  user GET    /users/:id(.:format) {:action=&gt;"show", :controller=&gt;"users"}
  </code></pre>