1

我有一个创建评论的字段(名为 pcomment)。我试图让它自动将 user_id 添加到 pcomment 表中的 pcomment 中,就像它自动添加 purchase_id 一样。我不确定为什么将 purchase_id 记录在数据库中,但每个 pcomment 的 user_id 仍然为空。这是 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作为资源,并且微帖子没有任何隐藏字段。相反,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 用户。pcomment 属于_to 用户和属于_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
4

1 回答 1

0

purchase.pcomments.build仅使用从 填充的 purchase_id 构建空的 Pcomment 对象purchase。要分配还user_id传递带有属性的哈希:

purchase.pcomments.build(:user_id => purchase.user_id)
于 2012-10-11T09:45:16.037 回答