I have a create product page and an add photo page. Add photo page should add photos to a product that was just created.
I can get to add photo page /products/:product_id/pics(.:format)
but I get an error on submit
ActiveRecord::RecordNotFound (Couldn't find Product without an ID):
photo controller
def create
@product = Product.find(params[:product_id]) # <--- error here
@photo = Photo.new
if @photo.valid?
@photo.product_id = @product.id
@photo.save!
respond_to do |format|
format.html { redirect_to product_path(@product) }
format.json { render json: @product }
end
else
redirect_to root_url, :notice => "Somehting went wrong!"
end
end
pics.html.haml
= form_for @photo, :html => { :multipart => true, :id => "fileupload" } do |f|
= f.file_field :upload
products controller
def pics
@product = Product.find(params[:product_id])
@photo = Photo.new
# @product.photos.build
end
full console error
Started POST "/photos" for 127.0.0.1 at 2013-07-09 02:11:11 -0400 Processing by PhotosController#create as JSON Parameters: {"utf8"=>"✓", "authenticity_token"=>"K9jWB2D0bFUB5+KOCRKLUsuDGNLchjzCBCL1h1znOiQ=", "photo"=>{"upload"=>#>}} Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Product without an ID): app/controllers/photos_controller.rb:15:in `create'
console with sachins solution
Started POST "/photos" for 127.0.0.1 at 2013-07-09 02:55:25 -0400 Processing by PhotosController#create as JSON Parameters: {"utf8"=>"✓", "authenticity_token"=>"5RV+GUCvNEFrw7l3/ApqAlbK/XJP78LmDR2Hc+O0rQ0=", "product_id"=>"125", "photo"=>{"upload"=>#>}} Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "125"]] Redirected to http://google.com/ Completed 302 Found in 4ms (ActiveRecord: 0.1ms)
Started GET "/" for 127.0.0.1 at 2013-07-09 02:55:25 -0400 Processing by StaticPagesController#home as JSON Rendered static_pages/home.html.haml within layouts/application (0.1ms) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" IS NULL LIMIT 1 Completed 200 OK in 93ms (Views: 91.8ms | ActiveRecord: 0.3ms)