best_in_place在rails 4中有多对多的关系

我已经制作了一个github回购,你可以在这里找到这个问题。 我有3个型号:

class User  { where(user_countries: {:event => true}) }, :through => :user_countries, :source => :country has_many :research_countries, -> { where(user_countries: {:research => true}) }, :through => :user_countries :source => :country end class UserCountry < ActiveRecord::Base belongs_to :country belongs_to :user end class Country < ActiveRecord::Base # ... end 

因此用户应该能够选择event_countries和research_countries。

这是我的用户控制器(没什么复杂的):

  class UsersController  [:id, :name]) end end 

这是我的用户显示页面:

  

event countries:

所以这里真的没什么复杂的。 我也可以成功编辑我的用户名,best_in_place工作正常。

问题是:如何编辑event_countries? 正如您所看到的,我尝试使用国家/地区的收集选项,但当我尝试选择国家时,我会得到以下内容:

  Processing by UsersController#update as JSON Parameters: {"user"=>{"event_countries"=>"3"}, "authenticity_token"=>"l5L5lXFmJFQ9kI/4klMmb5jDhjmtQXwn6amj1uwjSuE=", "id"=>"6"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]] (0.1ms) begin transaction (0.1ms) rollback transaction Completed 500 Internal Server Error in 3ms NoMethodError (undefined method `each' for nil:NilClass): app/controllers/users_controller.rb:17:in `update' 

我不明白它在做什么,我知道它必须是收集选项的问题。 如果您需要查看任何文件,请在此处查看我的回购: https : //github.com/spawnge/best_in_place_join_models_twice 。 我花了很多时间在这个任何答案/建议将不胜感激:)


更新:

我试过这个:

  

并且我已将event_country_ids添加到我的用户参数:

  params.require(:user).permit(:first_name, :event_country_ids) 

现在我可以看到所有的国家,但是当我选择一个这里是我得到的:

 Started PUT "/users/3" for 127.0.0.1 at 2014-12-18 01:19:27 +0000 Processing by UsersController#update as JSON Parameters: {"user"=>{"event_country_ids"=>"1"}, "authenticity_token"=>"aZAFIHgzdSL2tlFcGtyuu+XIJW3HX2fwQGHcB9+iYpI=", "id"=>"3"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]] (0.0ms) begin transaction Country Load (0.0ms) SELECT "countries".* FROM "countries" WHERE "countries"."id" = ? LIMIT 1 [["id", 1]] Country Load (0.0ms) SELECT "countries".* FROM "countries" INNER JOIN "user_countries" ON "countries"."id" = "user_countries"."country_id" WHERE "user_countries"."event" = 't' AND "user_countries"."user_id" = ? [["user_id", 3]] SQL (0.1ms) DELETE FROM "user_countries" WHERE "user_countries"."user_id" = ? AND "user_countries"."country_id" = 2 [["user_id", 3]] SQL (0.0ms) INSERT INTO "user_countries" ("country_id", "event", "user_id") VALUES (?, ?, ?) [["country_id", 1], ["event", "t"], ["user_id", 3]] (20.9ms) commit transaction Completed 204 No Content in 28ms (ActiveRecord: 21.3ms) 

如您所见,它似乎插入了正确的内容: INSERT INTO "user_countries" ("country_id", "event", "user_id") VALUES (?, ?, ?) [["country_id", 1], ["event", "t"], ["user_id", 3]]然而,在此之后我得到了Completed 204 No Content。 当我刷新页面输入为空时,我不明白。 有什么建议吗?


更新2:

我检查了控制台,它可以工作,我可以将event_countries添加到用户。 但是,当我刷新页面时,它不会显示用户的event_countries,我想这是因为我正在使用event_country_ids对象。

我认为以下代码应该工作:

 <%= best_in_place @user, :event_country_ids, as: :select, collection: Country.all.each_with_object({}) { |i, memo| memo[i.id] = i.name }, place_holder: "click here to edit", html_attrs: { multiple: true } %> 

假设您希望用户能够分配多个event_countries

参考