1

I want to create a custom route such as 'photos/:user_id/:photo_id' for showing each photo. In my routes file I have;

get 'photos/:user_id/:photo_id' => 'photos#show', :as => 'user_photo'

and rake routes gives me

user_photo GET       /photos/:user_id/:photo_id(.:format)   photos#show

Edit;

Now when I link to a photo using link_to 'some photo', user_photo_path(@photo,@user) I just get the following error

Processing by PhotosController#show as HTML
  Parameters: {"user_id"=>"8", "photo_id"=>"8"}
  User Load (0.6ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 8  ORDER BY "users"."id" ASC LIMIT 1
Completed 404 Not Found in 3ms

ActiveRecord::RecordNotFound - Couldn't find Photo without an ID:
  activerecord (4.1.1) lib/active_record/relation/finder_methods.rb:402:in `find_with_ids'
  activerecord (4.1.1) lib/active_record/relation/finder_methods.rb:68:in `find'
  activerecord (4.1.1) lib/active_record/querying.rb:3:in `find'
   () Users/batman/code/myapp/app/controllers/photos_controller.rb:73:in `set_photo'
1
  • 1
    try user_photo_path(photo_id: @photo.id, user_id: @photo.user_id) Commented Jun 19, 2014 at 8:55

2 Answers 2

4

You need to add it to params:

link_to 'some photo', user_photo_path(@photo.user, @photo)

The second error is caused by the way you are looking for a photo. Most likely you do:

photo = Photo.find(params[:id])

If that's the case you need either to change params[:id] to parmas[:photo_id] (may brake other actions if this is done in before_filter or change your route to:

get 'photos/:user_id/:id' => 'photos#show', :as => 'user_photo'
Sign up to request clarification or add additional context in comments.

5 Comments

user_id available in @photo variable
Your use of /id will cause an issue here bud!
Nice one - was going to recommend putting up the controller too
that's working now thanks, though I don't understand why using :photo_id causes the error and :id doesn't...?
@raphael_turtle - You have set_photo before_filter in your controller which is using params[:id]. This filter is run before the show action, which results in Photo.find(nil) - which is the cause of the issue.
1

I was writing this answer, so I'll continue

--

Routes

Shallow nesting could be a better way to do it:

#config/routes.rb
resources :users do 
    resources :photos, shallow: true #-> /user/photos/:id
end

--

Find

The problem you have (had) is that although you're sending the right params, your controller will likely not be able to build a model with the code you have.

BroiStatse mentioned you should change Photo.find(params[:id]) - the reason for this is that when you use Model.find ___, you're telling the Model to look up the primary key in your datatable based on the values you pass to it

Using params[:id] is fine if you have the id param set (as it is most of the time). However, you don't have it set, leading to your controller being unable to find the record:

Parameters: {"user_id"=>"8", "photo_id"=>"8"}

These are the params you're receiving - your error is thus:

ActiveRecord::RecordNotFound - Couldn't find Photo without an ID

Basically means you've not a param set for the find method. To fix this, you should use the photo_id param:

#app/controllers/photos_controller.rb
def show
    photo = Photo.find params[:photo_id]
end

--

Params

When you request a controller action in your application, you'll basically be sending Rails a series of params which it can then use in the controller.

These params are part of a hash, which you'll be able to call with params[:id] etc:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.