2012-10-11 42 views
1

我有一個創建註釋的字段(名爲pcomment)。我試圖讓它自動將user_id添加到pcomment表中的pcomment,就像它自動添加purchase_id一樣。我不確定爲什麼purchase_id被記錄在數據庫中,但user_id對於每個pcomment都保持空白。這是該評論的表格。爲什麼在創建評論時不會自動添加user_id?

<%= 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作爲資源,並且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 

,我知道它不工作的原因是,我檢查數據庫和pcomment成功地與填充包括purchase_id所有列創建,但USER_ID仍是空白。此外,用戶has_many pcomments和has_many購買。購買has_many pcomments和belongs_to用戶。該評論屬於用戶且屬於購買。

也,這裏是

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

purchase.pcomments.build建立空Pcomment對象只是purchase_id從purchase充滿pcomments_controller.rb。也要分配user_id通過散列屬性:

purchase.pcomments.build(:user_id => purchase.user_id) 
+0

我已經在問題中添加了我的pcomments控制器。這是你的意思嗎? –

+0

我已經講過了'<%= form_for([purchase,purchase.pcomments.build(:user_id => purchase.user_id)],:html => {:id =>「blah_form」})do | f | %>'。 Pcomment對象構建在表單中,然後使用'POST'參數傳遞。所以在params [:pcomment]中將會有'user_id'和'purchase_id'。 –