我有一個表格測試,其中包含字段'user_id'和'post_id'。 user_id我從帖子表中通過current_user.id和post_id獲取。爲此,我做的事: 在測試控制器: -獲取身份證表格另一個表格,導軌
def create
@user = current_user
@test = Test.new(params[:test])
@post = Post.find(params[:id])
@test.post_id = @post.id
@test.user_id = @user.id
respond_to do |format|
@test.save
format.html { redirect_to(:back, :notice => "successfull") }
else
format.html { render :action => "new" }
end
end
在我的模型,我創建了關聯關係: -
在test.rb:
belongs_to :post
在post.rb:
has_many :test
查看頁面爲:
= form_for @test, :validate => true do |f|
.span
= f.text_field :email, :placeholder =>"Email Address"
.span
= f.label :name
= f.text_field :name
.span
= f.submit "Save"
user_ID的正確保存但POST_ID它給錯誤:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
我怎樣才能解決這個問題?
你確定你應該只當你訪問'create'方法是登錄即'current_user'不應爲零 – Salil 2013-04-25 08:23:13