Search code examples
ruby-on-railsruby-on-rails-4friendly-idnested-resources

Ruby on Rails 4 - friendly_id with nested routed


I have a nested rout for toys and then toys will have reviews. The toys works fine with friendly_id but going to website.com/toys/toy-name/reviews/new brings up

Couldn't find Toy without an ID

My routes file looks like this

 resources :toys do
   resources :reviews, except: [:show, :index]
 end

The reviews controller has a set_toy method that is called before_action and is where the error is occurring. The method looks like

 def set_toy
  @toy = Toy.friendly.find(params[:id])
end

The toys controller has a similar method called toy_params that has exactly the same contents.

I can't see why the reviews controller cannot find the toy ID as I have passed it in with friendly.find.

Thank you


Solution

  • You will need

     def set_toy
       @toy = Toy.friendly.find(params[:toy_id])
     end
    

    in your reviews controller.