0
votes

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)

5

5 Answers

2
votes

try this out: ---

photos controller

def new
  @product = Product.find(params[:product_id])
  @photo = Photo.new 
  @photo.product_id = @product.id
end

pics.html.haml

= form_for @photo, :html => { :multipart => true, :id => "fileupload"  } do |f|
  = f.file_field :upload
  = hidden_field_tag 'product_id', @photo.product_id
2
votes

Use form_for [@product, @photo] instead of just @photo in your form. Be sure to, of course, find the product using params[:product_id].

You need to nest your routes like this:

resources :products do
  resources :photos
end

Otherwise you won't have a params[:product_id] on your request.

2
votes

try in your form

form_for [@product, @photo] 
1
votes

error is there in accessing the product_id from params

use params[:product_id] instead params[:product][:product_id]

0
votes
Just set the hidden_field_tag in form_for
 eg:-

= hidden_field_tag 'product_id', @product.id