我有一个创建评论的字段(名为pcomment)。我试图让它自动将user_id添加到pcomment表中的pcomment,就像它自动添加purchase_id一样。我不确定为什么purchase_id正在数据库中记录,但user_id对于每个pcomment仍然是空白的。这是pcomment的形式。
<%= form_for([purchase, purchase.pcomments.build], :html => { :id => "blah_form" }) do |f| %>
<div class="field">
<h4>What deal are you offering?</h4>
<%= f.text_field :body %>
</div>
<% end %>
可能我需要添加一些hidden_field,但我不这么认为。我使用http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#cha-user_microposts作为资源,并且微博没有任何hidden_field。相反,user_id被索引并且在创建微博时自动创建(基于当时登录的人)。这部分对我也有用,在我的理性中添加了对pcomments表上的索引user_id足以自动生成它。这是我的schema.rb文件,以便您可以看到我的数据库的当前状态。
ActiveRecord::Schema.define(:version => 20121011085147) do
create_table "pcomments", :force => true do |t|
t.string "body"
t.integer "purchase_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "pcomments", ["purchase_id"], :name => "index_pcomments_on_purchase_id"
add_index "pcomments", ["user_id"], :name => "index_pcomments_on_user_id"
create_table "purchases", :force => true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "purchases", ["user_id", "created_at"], :name => "index_purchases_on_user_id_and_created_at"
create_table "sales", :force => true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "sales", ["user_id", "created_at"], :name => "index_sales_on_user_id_and_created_at"
create_table "scomments", :force => true do |t|
t.string "body"
t.integer "sale_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "scomments", ["sale_id"], :name => "index_scomments_on_sale_id"
add_index "scomments", ["user_id"], :name => "index_scomments_on_user_id"
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
end
以及我知道它不起作用的原因是我在数据库中检查并且成功创建了包含所有填充列的包括purchase_id但是user_id仍为空白的命令。此外,用户还有许多小商品和很多商品。购买has_many pcomments和belongs_to用户。 pcomment属于用户和belongs_to购买。
另外,这里是pcomments_controller.rb
class PcommentsController < ApplicationController
before_filter :signed_in_user
def create
@purchase = Purchase.find(params[:purchase_id])
@pcomment = @purchase.pcomments.build(params[:pcomment], :user_id => @purchase.user_id)
@pcomment.purchase = @purchase
if @pcomment.save
flash[:success] = "Offer submited!"
redirect_to :back
else
render 'shared/_pcomment_form'
end
end
def new
@pcomment=purchase.pcomments.new
end
end
def new
@pcomment=purchase.pcomments.new(:user_id => purchase.user_id)
end
end
答案 0 :(得分:0)
purchase.pcomments.build
仅使用从purchase
填充的purchase_id构建空的Pcomment对象。同时分配user_id
也会使用属性传递哈希:
purchase.pcomments.build(:user_id => purchase.user_id)