Ruby on rails 设置有很多:通过rails 3上的表单

Ruby on rails 设置有很多:通过rails 3上的表单,ruby-on-rails,ruby-on-rails-3,has-many-through,Ruby On Rails,Ruby On Rails 3,Has Many Through,我正在尝试使用表单为我的用户创建用户角色 <%= form_for @user do |f| %> <%= f.error_messages %> <p> <%= f.label :username %><br /> <%= f.text_field :username %> </p> <p> <%= f.label :email, "Email Addr

我正在尝试使用表单为我的用户创建用户角色

<%= 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 %>
当我发送表格时,我收到错误信息:

ActiveRecord::UsersController#create中的AssociationTypeMismatch

应为角色(#703316817580),获取字符串(#70331650003400)

请求
参数:
{“utf8”=>“✓",
“真实性令牌”=>“WHpOW+DmymZ2pWmY9NHSuodf2vjyKdgMNZcc8NvCNa0=”,
“用户”=>{“用户名”=>“ioio”,
“电子邮件”=>”ioio@ioio.com",
“密码”=>“[已筛选]”,
“密码确认”=>“[已筛选]”,

“roles”=>[“2”]},#####错误消息提示您:为了能够保存用户对象,您首先需要以某种方式创建关联的角色对象。目前,您只有一个作为角色ID的字符串数组


您需要在用户模型上使用该方法。

错误消息提示您:为了能够保存用户对象,您首先需要以某种方式创建关联的角色对象。目前,您只有一个作为角色ID的字符串数组


您需要在用户模型上使用该方法。

使用当前表单并基于:

<%= check_box_tag :role_ids, role.id, @user.roles.include?(role), :name => 'user[role_ids][]' -%>
……同样:

   def update
     @user = User.where(:username=>params[:id]).first
     roles = Role.find(params[:user][:role_ids]) rescue []
     @user.roles = roles
     if @user.update_attributes(params[:user])
       redirect_to users_url, :notice  => "Successfully updated user."
     else
      render :action => 'edit'
     end
   end

使用您当前的表格并基于:

<%= check_box_tag :role_ids, role.id, @user.roles.include?(role), :name => 'user[role_ids][]' -%>
……同样:

   def update
     @user = User.where(:username=>params[:id]).first
     roles = Role.find(params[:user][:role_ids]) rescue []
     @user.roles = roles
     if @user.update_attributes(params[:user])
       redirect_to users_url, :notice  => "Successfully updated user."
     else
      render :action => 'edit'
     end
   end

我试图为用户模型(User.rb)中的角色添加accepts_nested_attributes_,但运气不好。@Mr.Gando查看@miked answer-他正确地指出,您需要
角色ID
,而不是
角色
,作为参数散列(和复选框)中的键。然后,您可以通过在用户模型上为
使用
accepts\u nested\u attributes\u进一步简化代码。我尝试在用户模型(user.rb)中为角色添加accepts\u nested\u attributes\u,但没有运气。@Mr.Gando看@miked answer-他正确地指出,您需要
角色ID
,而不是
角色
,作为参数散列(和复选框)中的键。然后,您可以通过在用户模型上使用
接受
的嵌套属性来进一步简化代码。解决方案成功了吗?修复对我有效,所以我很好奇它是否也解决了您的问题。解决方案成功了吗?修复对我有效,所以我很好奇它是否也解决了您的问题。
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"WHpOW+DmymZ2pWmY9NHSuodf2vjyKdgMNZcc8NvCNa0=",
 "user"=>{"username"=>"ioio",
 "email"=>"ioio@ioio.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "role_ids"=>["2"]},   #### <<< ==== This is the checkbox that I selected for this request.
 "commit"=>"Sign up"}
    def create
      @user = User.new(params[:user])
      roles = Role.find(params[:user][:role_ids]) rescue []
      @user.roles = roles
      if @user.save
        session[:user_id] = @user.id
        render :action => 'dashboard'
      else
        render :action => 'new'
      end
    end
   def update
     @user = User.where(:username=>params[:id]).first
     roles = Role.find(params[:user][:role_ids]) rescue []
     @user.roles = roles
     if @user.update_attributes(params[:user])
       redirect_to users_url, :notice  => "Successfully updated user."
     else
      render :action => 'edit'
     end
   end