Tony Beninate
Tony Beninate

Reputation: 1985

Rails properly pass in id through form

I just need some clarity of thought on this one. I've got a Photos show page where a user can click "Like" to create a new like record for a particular photo.

On my photos show.html page I have the following form:

<%= form_for :like, :url => likes_path do |f| %>
  <%= hidden_field_tag(:photo_id, @photo.id) %>
  <%= f.submit "Like" %>
<% end %>

When I click the form I get a message that it "Couldn't find a Photo without an ID":

Started POST "/likes" for 127.0.0.1 at Sat Feb 25 16:43:21 -0500 2012
Processing by LikesController#create as HTML
Parameters: {"commit"=>"Like", "authenticity_token"=>"cQnuAHb/f6Sgo3aB5xPKErx3joQTV+DHGs0w9vi13vM=", "utf8"=>"\342\234\223", "photo_id"=>"47"}
Completed 404 Not Found in 68ms

ActiveRecord::RecordNotFound (Couldn't find Photo without an ID):
  app/controllers/likes_controller.rb:8:in `create'

Not sure why when it seems to be passing in the photo_id correctly, no? Here's a look at my likes_controller:

def create
  @photo = Photo.find(params[:id])
  @like = Like.new(:photo_id => :photo_id, :user_id => current_user.id)
end

Anyone see what I'm doing wrong here? Thanks.

Upvotes: 0

Views: 578

Answers (1)

Baldrick
Baldrick

Reputation: 24340

It should work with Photo.find(params[:photo_id]).

The error message says that you called Photo.findwith a nil parameter, e.g. there is no parameter id in the request.

Upvotes: 1

Related Questions