2015-12-26 72 views
0

我新的軌道4,我使用嵌套屬性的多個圖像upload.But我遇到這種的ActiveRecord :: StatementInvalid在產品#指數

即時得到的ActiveRecord :: StatementInvalid在產品#幾個問題指數

Mysql2 ::錯誤:SELECT pictures:未知列在 'where子句' pictures.product_id「。 FROM pictures WHERE picturesproduct_id = 11 *錯誤

我的型號如下

class Picture < ActiveRecord::Base 
    belongs_to :product 
    has_attached_file :image 
end 

class Product < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :category 
    has_many :comments , dependent: :destroy 
    has_many :pictures 
    accepts_nested_attributes_for :pictures 
end 

products_controller.rb

class ProductsController < ApplicationController 
def show 
    @product = Product.find(params[:id]) 
    @comment = @product.comments.build 
    @category_id = @product.category_id 
    @category1 = Category.find_by(id: @category_id) 
end 
def new 
    @product = current_user.products.build 
    @product.pictures.build 
end 
def create 
    @product = current_user.Product.new(product_params) 
    @product.save 
respond_to do |format| 
    if @product.save 
    format.html { redirect_to @product, notice: 'Product was successfully created.' } 
    format.json { render :show, status: :created, location: @product } 
    else 
    format.html { render :new } 
    format.json { render json: @product.errors, status: :unprocessable_entity } 
     end 
    end 
end 
private 
# Use callbacks to share common setup or constraints between actions. 
def set_product 
    @product = Product.find(params[:id]) 
end 
def product_params 
    params.require(:product).permit(:name, :price, :description, :reason, :user_id,:status,:category_id,pictures_attributes: [:image]) 
end 

def correct_user 
    @product = current_user.products.find_by(id: params[:id]) 
    redirect_to root_url if @product.nil? 
end 
end 

Schema.rb

ActiveRecord::Schema.define(version: 20151226132302) do 
create_table "pictures", force: :cascade do |t| 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
end 

create_table "products", force: :cascade do |t| 
t.datetime "created_at",      null: false 
t.datetime "updated_at",      null: false 
t.string "name",    limit: 255 
t.integer "price",    limit: 4 
t.text  "description",  limit: 65535 
t.text  "reason",    limit: 65535 
t.integer "user_id",   limit: 4 
t.string "image_file_name", limit: 255 
t.string "image_content_type", limit: 255 
t.integer "image_file_size", limit: 4 
t.datetime "image_updated_at" 
t.string "status",    limit: 255 
t.integer "category_id",  limit: 4 
t.integer "product_id",   limit: 4 
end 
end 

我的遷移文件 20151226132302_add_product_id_to_product.rb

class AddProductIdToPictures < ActiveRecord::Migration 
    def change 
     add_reference :pictures, :product, index: true 
    end 
    end 

即使有上述遷移的product_id不會添加到照片模式。 有人可以幫助我嗎? 這將是有益的,如果有人可以給我很好的參考爲軌道4

回答

2
​​

意味着你沒有product_id列在您的pictures表。

如果您使用has_many/belongs_to,你需要設置foreign_key在其表的belongs_to型號:

enter image description here

如果沒有product_id列在你的pictures表,你需要使用以下命令:

$ rails g migration AddProductIdToPictures 

# db/migrate/add_product_id_to_pictures_________.rb 
class AddProductIdToPictures < ActiveRecord::Migration 
    def change 
     add_reference :pictures, :product, index: true 
    end 
end 

$ rake db:migrate 

編號:Add a reference column migration in Rails 4


您可能遇到的另一個問題是您在新方法中使用了.build。我知道newbuild具有非常小的差異,但我的印象是使用new

def new 
    @product = current_user.products.new 
    @product.pictures.build 
end 

你還得到了一個錯誤在你create行動:

def create 
    #NO - @product = current_user.Product.new(product_params) #-> cannot use constant 

    #Should be this: 
    @product = current_user.products.new 
    @product.save 
end 
+0

比你了我的問題解決了 –

相關問題