【问题标题】:rails 3 has_and_belongs_to_many doesn't assign recordrails 3 has_and_belongs_to_many 没有分配记录
【发布时间】:2013-09-10 11:36:33
【问题描述】:

我有两个型号UserConf。他们有has_and_belongs_to_many 关系。当我尝试创建一个新的 conf 时,应用程序将其记录到 confs 表而不是 confs_users 表。所以不会将新记录分配给用户。

conf.rb

class Conf < ActiveRecord::Base
  attr_accessible :id, :linear_axis_number, :control_unit_brand, :control_unit_model, :description, 
              :machine_brand, :machine_model, :milling_mode, :rotary_axis_number, :tool_axis_x, :tool_axis_y, 
              :tool_axis_z, :turning_mode, :machine_name, :developer_id, :xml, :user_id

  has_and_belongs_to_many :users
  belongs_to :developer, :class_name => 'User', :foreign_key => 'developer_id'

  has_attached_file :xml, :url => "downloads/:attachment/:id/:basename.:extension", :path => ":rails_root/downloads/:attachment/:id/:basename.:extension"
  attr_protected :xml_file_name, :xml_content_type, :xml_file_size
end

用户.rb

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation, :developer, :admin, :company_id, :boss_id
  belongs_to :company
  has_and_belongs_to_many :confs
  has_secure_password
end

confs_controller.rb

class ConfsController < ApplicationController
  before_filter  :signed_in_user, only:[:index, :edit, :update, :destroy]
  before_filter  :developer_user, only: :destroy
  def new
    @users = User.where(:boss_id => current_user.id)
    @conf = Conf.new
  end

  def create
    @conf = Conf.new(conf_params)    
    if @conf.save
      flash[:success] = "New Configuration uploaded!"
      redirect_to conf_show_own_path
    else
      @users = User.where(:boss_id => current_user.id)
      flash[:error] = "There is a problem!"
      render 'new'
    end
  end

  private
  def conf_params
    params.require(:conf).permit( :id, :linear_axis_number, :control_unit_brand, :control_unit_model, :xml,
                              :description, :machine_brand, :machine_model, :milling_mode, :developer_id,
                              :rotary_axis_number, :tool_axis_x, :tool_axis_y, :tool_axis_z, :turning_mode, 
                              :machine_name, :user_id) if params[:conf]
  end
end

new.html.erb

<%= form_for @conf, :html => {:multipart => true} do |f| %>
    <%= f.label :machine_name %>
    <%= f.text_field :machine_name %>
    .....
    <% @users.each do |g| %>
        <%= check_box_tag 'conf[user_id][]', g.id, false, :id => g.name %>
        <%= label_tag g.name %>
    <% end %>

    <%= f.submit "Upload", class: "btn btn-large btn-primary" %>
<% end %>

我想我应该添加@conf.users &lt;&lt; @user 之类的内容,但是因为我正在借助复选框确定在填写表单期间将分配哪些用户,所以我不知道该怎么做。

【问题讨论】:

    标签: ruby-on-rails ruby forms mass-assignment has-and-belongs-to-many


    【解决方案1】:

    您可以一次性分配所有用户:

    @conf.users << User.find(params[:conf][:user_id])
    

    【讨论】:

    • 不客气。为避免混淆,我还将复选框重命名为 user_ids。
    • 当我这样做时,我在这行 @conf.users &lt;&lt; User.find(params[:conf][:user_id]) 中收到错误 ActiveRecord::RecordNotFound in ConfsController#create Couldn't find User without an ID 在单数版本中它似乎运行良好
    • 您也需要在此处将 :user_id 更改为 :user_ids 。否则 params[blah] 返回 null。 :)
    • 好的,我改了,它又能用了。有s和没有s有什么区别?
    • user_id 建议它只包含一个 id。这不是代码更改,只是让其他人更容易理解那里发生的事情。
    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多