Ruby on rails 获取ActiveRecord::PostsController中的AssociationTypeMismatch#在Rails应用程序中创建

Ruby on rails 获取ActiveRecord::PostsController中的AssociationTypeMismatch#在Rails应用程序中创建,ruby-on-rails,ruby,activerecord,devise,ruby-on-rails-4.2,Ruby On Rails,Ruby,Activerecord,Devise,Ruby On Rails 4.2,完整跟踪activerecord(4.2.4)lib/active\u record/associations/association.rb:218:inraise\u on\u type\u不匹配 activerecord(4.2.4)lib/active\u record/associations/属于\u association.rb:12:in'replace' activerecord(4.2.4)lib/active_record/associations/singular_asso

完整跟踪activerecord(4.2.4)lib/active\u record/associations/association.rb:218:in
raise\u on\u type\u不匹配
activerecord(4.2.4)lib/active\u record/associations/属于\u association.rb:12:in'replace'
activerecord(4.2.4)lib/active_record/associations/singular_association.rb:17:in
writer' activerecord(4.2.4)lib/active_record/associations/builder/association.rb:123:in
start=
activerecord(4.2.4)lib/active\u record/attribute\u assignment.rb:54:in
public\u send' activerecord(4.2.4)lib/active\u record/attribute\u assignment.rb:54:in
\u assign\u attribute'
activerecord(4.2.4)lib/active\u record/attribute\u assignment.rb:41:in
块中的assign\u attributes' actionpack(4.2.4)lib/action\u controller/metal/strong\u参数。rb:185:in
每个\u对'
actionpack(4.2.4)lib/action\u controller/metal/strong\u参数。rb:185:in
每对 activerecord(4.2.4)lib/active\u record/attribute\u assignment.rb:35:in
assign\u attributes'
activerecord(4.2.4)lib/active_record/core.rb:564:in
init_属性' activerecord(4.2.4)lib/active_record/core.rb:281:in
initialize'
activerecord(4.2.4)lib/active_record/heritation.rb:61:in
new' activerecord(4.2.4)lib/active_record/heritation.rb:61:in
new'
activerecord(4.2.4)lib/active_record/persistence.rb:33:in
create' app/controllers/posts_controller.rb:8:in'create'

post.rb

Started POST "/posts" for ::1 at 2016-01-16 20:55:15 -0800 
Processing by PostsController#create as HTMLParameters: {"utf8"=>"✓", "authenticity_token"=>"RZREVs4Wy7ZWvo4u0xE5OmrawV2MYtxJNJZDfs+eQY8o2n1RthJsL5c1MklIpP+XlMGNbDYSD49fjna7Szq/1g==", "post"=>{"start"=>"2", "end"=>"1", "when(1i)"=>"2016", "when(2i)"=>"1", "when(3i)"=>"17", "when(4i)"=>"06", "when(5i)"=>"00"}}
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.2ms)
ActiveRecord::AssociationTypeMismatch (Location(#70172044981120) expected, got String(#70171989781540)):app/controllers/posts_controller.rb:8:in `create'
new.html.erb

private
  def post_params
        params.require(:post).permit(:start, :end, :when, :description, :seats, :round_trip).merge(user_id: current_user.id)
  end

我真的不知道我为什么会遇到这个问题。柱具有起点和终点位置。它还有一个用户id,用户已通过Desive登录。请提供帮助。

这不起作用,因为您正在将字符串传递给接受Location类实例的属性。由于“开始”和“结束”在模型中显示为类位置,因此您的表单要么使用字段_嵌套属性,要么选择位置。如果有嵌套属性,则需要添加

ActiveRecord::Schema.define(version: 20160117041612) do
  create_table "locations", force: :cascade do |t|
    t.string   "city_name"
    t.string   "state_name"
    t.string   "zip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
    create_table "posts", force: :cascade do |t|
      t.integer  "start"
      t.integer  "end"
      t.datetime "when"
      t.text     "description"
      t.integer  "seats"
      t.integer  "user_id"
      t.integer  "round_trip"
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
    end

  create_table "users", force: :cascade do |t|
    t.string   "first_name",             default: "", null: false
    t.string   "last_name",              default: "", null: false
    t.integer  "location_id"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
到您的Post模型,然后更新Post_参数以获得正确的参数

accepts_nested_attributes_for :start, :end
更新:
现在你已经发布了你的表格,我们可以看到你有选择。您的模式还显示posts模型中的开始和结束都是整数。通常情况下,它们应该是start_id和end_id,或者您需要使用:foreign_key选项和bellings to。另外,您的两个选择当前正在传递参数,即start_location和end_location,这两个参数对于任何模型都无效。如果您修复了start\u id/end\u id的命名,那么您将更新如下选择

def post_params
  params.require(:post).permit(:start_id, :end_id, :when, :description, :seats, :round_trip).merge(user_id: current_user.id)
end
“选择一个”}%>
并更新您的post_参数以允许start_id和end_id。如果改为使用:foreign_key选项,则

<%= f.select :start_id, Location.all.collect {|x [x.city_name, x.id]}, {:include_blank => 'Select One'} %>
“选择一个”}%>

这不起作用,因为您正在将字符串传递给接受Location类实例的属性。由于“开始”和“结束”在模型中显示为类位置,因此您的表单要么使用字段_嵌套属性,要么选择位置。如果有嵌套属性,则需要添加

ActiveRecord::Schema.define(version: 20160117041612) do
  create_table "locations", force: :cascade do |t|
    t.string   "city_name"
    t.string   "state_name"
    t.string   "zip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
    create_table "posts", force: :cascade do |t|
      t.integer  "start"
      t.integer  "end"
      t.datetime "when"
      t.text     "description"
      t.integer  "seats"
      t.integer  "user_id"
      t.integer  "round_trip"
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
    end

  create_table "users", force: :cascade do |t|
    t.string   "first_name",             default: "", null: false
    t.string   "last_name",              default: "", null: false
    t.integer  "location_id"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
到您的Post模型,然后更新Post_参数以获得正确的参数

accepts_nested_attributes_for :start, :end
更新:
现在你已经发布了你的表格,我们可以看到你有选择。您的模式还显示posts模型中的开始和结束都是整数。通常情况下,它们应该是start_id和end_id,或者您需要使用:foreign_key选项和bellings to。另外,您的两个选择当前正在传递参数,即start_location和end_location,这两个参数对于任何模型都无效。如果您修复了start\u id/end\u id的命名,那么您将更新如下选择

def post_params
  params.require(:post).permit(:start_id, :end_id, :when, :description, :seats, :round_trip).merge(user_id: current_user.id)
end
“选择一个”}%>
并更新您的post_参数以允许start_id和end_id。如果改为使用:foreign_key选项,则

<%= f.select :start_id, Location.all.collect {|x [x.city_name, x.id]}, {:include_blank => 'Select One'} %>
“选择一个”}%>

“end”在ruby中是一个保留关键字:您是否尝试将
:end
重命名为其他名称(例如
:end\u location
)?很遗憾,这不起作用。您是否也可以添加查看代码?我假设开始、结束和时间都是日期还是日期时间?我已经添加了视图代码。开始位置、结束位置和时间(日期时间类型)都在视图代码中设置或至少应该设置。“结束”是ruby中的保留关键字:您是否尝试将
:结束
重命名为其他名称(例如
:结束位置
)?不幸的是,这不起作用。您还可以添加视图代码吗?我假设开始、结束和时间都是日期还是日期时间?我已经添加了视图代码。开始位置、结束位置和时间(日期时间类型)都在视图代码中设置,或者至少应该设置。
<%= f.select :start, Location.all.collect {|x [x.city_name, x.id]}, {:include_blank => 'Select One'} %>