0

I have two models User and Conf. They have has_and_belongs_to_many relationship. When I try to create a new conf, app is recording it to confs table but not confs_users table. So doesn't assign new record to 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

user.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 %>

I think I should add something like @conf.users << @user, but because I am determining which users will be assigned during filling the form with the help of checkboxes, I don't know how to do.

kalahari
  • 895
  • 5
  • 15
  • 34

1 Answers1

2

You can assign all the users in one go:

@conf.users << User.find(params[:conf][:user_id])
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • You're welcome. To avoid confusion I would also rename checkboxes to user_ids. – BroiSatse Sep 10 '13 at 12:32
  • When I do it, I am getting an error `ActiveRecord::RecordNotFound in ConfsController#create Couldn't find User without an ID` in this line `@conf.users << User.find(params[:conf][:user_id])` in singular version it seems to work well – kalahari Sep 10 '13 at 12:41
  • you need to change :user_id to :user_ids here as well. Otherwise params[blah] returns null. :) – BroiSatse Sep 10 '13 at 13:19
  • ok, I changed, it works again. What is the difference with or without s? – kalahari Sep 10 '13 at 13:36
  • user_id suggest it contains only one id. This is not a code change, just it will be easier for other to understand what's going on there. – BroiSatse Sep 10 '13 at 13:48