有许多best_in_place在轨道4一对多的关系 [英] best_in_place with a many to many relationship in rails 4
问题描述
我已经一个GitHub库,你可以在这里找到的只是为这个问题。我有3个型号:
I have made a github repo that you can find here just for this question. I have 3 models:
class User < ActiveRecord::Base
has_many :user_countries
has_many :event_countries,
-> { 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。
So a user should be able to choose event_countries and research_countries.
这里是我的用户控制器(没有什么复杂的):
here's my users controller (nothing complicated):
class UsersController < ApplicationController
respond_to :html, :json
before_action :get_user, only: [:show, :edit, :update]
before_action :get_users, only: [:index]
def index
end
def show
end
def edit
end
def update
@user.update_attributes(user_params)
respond_with @user
end
private
def get_user
@user = User.find(params[:id])
end
def get_users
@users = User.all
end
def user_params
params.require(:user).permit(:first_name, :event_countries => [:id, :name])
end
end
这是我的用户显示页面:
And here's my user show page:
<%= best_in_place @user, :first_name %>
<p> event countries: </p>
<%= best_in_place @user, :event_countries, place_holder: "click here to edit", as: :select, collection: Country.all.map {|i| i.name} %>
<%= link_to "users index", users_path %>
所以没什么好说的,在这里复杂的。我也可以成功地编辑自己的用户名,best_in_place工作正常。
So there's really nothing complicated here. I can also succesfully edit my users first name, best_in_place is working fine.
现在的问题是:我怎么编辑event_countries?正如你可以看到我试图用同国家收集选项,但是当我尝试选择一个国家,我得到如下:
The question is: how do I edit the event_countries ? As you can see I tried to use the collection option with the countries but when I try to select a country I get the following:
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 。我有一个花了很多时间在这任何答案/建议,将大大AP preciated:)
I don't understand what it's doing, I know it must be a problem with the collection option. If you need to see any file please check my repo here: https://github.com/spawnge/best_in_place_join_models_twice . I have a spent a lot of time on this any answer/suggestion would be greatly appreciated :)
更新:
我已经试过这样:
<%= best_in_place @user, :event_country_ids, as: :select, collection: Country.all.map { |i| i.name }, place_holder: "click here to edit", html_attrs: { multiple: true } %>
和我已经加入:event_country_ids到我的用户PARAMS:
and I have added :event_country_ids to my user params:
params.require(:user).permit(:first_name, :event_country_ids)
现在我可以看到所有的国家,但是当我选择一个在这里就是我得到:
And now I can see all the countries but when I select one here's what I get:
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 INTOuser_countries(COUNTRY_ID,事件,USER_ID)VALUES [ COUNTRY_ID,1],[事件,T],[user_ID的,3]
但是我得到的已完成204无内容只是在那之后。我不明白,当我刷新页面输入是空的。任何建议?
As you can see it seems that it insert the right content: INSERT INTO "user_countries" ("country_id", "event", "user_id") VALUES (?, ?, ?) [["country_id", 1], ["event", "t"], ["user_id", 3]]
However I get the Completed 204 No Content just after that. I don't understand when I refresh the page the input is empty. Any suggestion ?
更新2:
我检查的控制台和它的作品,我可以添加event_countries给用户。然而,它不会显示在用户的event_countries当我刷新页面,我想这是因为我使用了event_country_ids对象。
I checked in the console and it works, I can add event_countries to a user. However it doesn't display the user's event_countries when I refresh the page, I guess that's because I'm using the event_country_ids object.
推荐答案
我觉得下面的code应该工作:
I think the following code should work:
<%= 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
。
参考
- <一个href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many,特别是通过
的has_many
创建的collection_singular_ids =
方法。 - https://github.com/bernat/best_in_place#select ,的结构收集需要一个哈希值。对于每个
键=>值
对的,关键是什么提交的形式和价值是什么显示给用户。 - http://ruby-doc.org/核心2.1.5 / Enumerable.html#方法-I-each_with_object ,
each_with_object
是核心Ruby库的一部分。
- http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many, specifically the
collection_singular_ids=
method created byhas_many
. - https://github.com/bernat/best_in_place#select, the structure of the collection needs to be a hash. For each
key => value
pair, the key is what's submitted with the form and the value is what's displayed to the user. - http://ruby-doc.org/core-2.1.5/Enumerable.html#method-i-each_with_object,
each_with_object
is a part of the core Ruby library.
这篇关于有许多best_in_place在轨道4一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!