我试图创建使用形式为我的用户的用户角色,设置的has_many:通过与在轨道上的表单3
<%= form_for @user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, "Email Address" %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation, "Confirm Password" %><br />
<%= f.password_field :password_confirmation %>
</p>
<% # f.select :roles, Role.all.map {|r| [r.title]} %>
<% Role.all.each do |role| %>
<div>
<%= check_box_tag :role_ids, role.id, @user.roles.include?(role), :name => 'user[role_ids][]' -%>
<%= label_tag :role_ids, role.title -%>
</div>
<% end -%>
<p><%= f.submit (@user.new_record? ? "Sign up" : "Update"), :id => :sign_up %></p>
<% end %>
这是我在我的模型协会
class user < ActiveRecord::Base
has_many :assignments
has_many :roles, :through => :assignments
end
class assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
class role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
通过使用我在开始介绍的表单创建用户和角色之间的分配的方式是什么?
我在我的用户控制器创建操作是这样的:
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
render :action => 'dashboard'
else
render :action => 'new'
end
end
当我把我的形式,我得到的错误:
的ActiveRecord :: AssociationTypeMismatch在UsersController#创建
角色(# 70331681817580)预计得到String(#70331650003400)
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"WHpOW+DmymZ2pWmY9NHSuodf2vjyKdgMNZcc8NvCNa0=",
"user"=>{"username"=>"ioio",
"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"roles"=>["2"]}, #### <<< ==== This is the checkbox that I selected for this request.
"commit"=>"Sign up"}
欢迎任何帮助。
运气好的解决方案?该修补程序适用于我,所以我很好奇它是否也解决了您的问题。 – miked 2012-02-02 19:21:07