![](/img/trans.png)
[英]create method adding 2 values and assigning to to an attribute in rails table
[英]Adding attribute values to table record in rails
我想在product #create action時插入user_id。
如何在productController中插入user_id?
用戶只是模特。
這是products_controller.rb
def create
@user = current_user
@product = Product.new(product_params, user_id: user_id)
respond_to do |format|
if @product.save
format.html { redirect_to wardrobe_items_path, 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
產品表
class AddUserIdToProducts < ActiveRecord::Migration
def change
add_column :products, :user_id, :string
end
end
做這個
@product = Product.new(product_params)
@product.user_id = current_user.id
做這個...
def create
@user = current_user
@product = Product.new(product_params)
@product.user_id = current_user.id
respond_to do |format|
if @product.save
format.html { redirect_to wardrobe_items_path, 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.