0

I'm a RoR newbie. I'm not able to create a new employee with assigned role. From the serverlog below I sort of figured that the record is not getting saved because "role_ids" is blank but I cannot fathom why "role_ids" is passed instead of "role"

I'm feel this is probably a one line fix. Please help me figure the issue. Thanks in advance for your time.

Processing by EmployeesController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"Nf9Ti9vdr0ErvJV3LKoErT6dMoUwWAI5eJVBOUISZxo=", "employee"=> {"name"=>"dvxzv", "role_ids"=>["", "4"], "reports_to"=>"Adam"}, "commit"=>"Create Employee"}
 (0.1ms)  begin transaction
 (0.0ms)  rollback transaction
 Role Load (0.2ms)  SELECT "roles".* FROM "roles" ORDER BY title
 Role Load (0.1ms)  SELECT "roles".* FROM "roles" 
 Rendered employees/_form.html.erb (8.9ms)

Notice the "role_ids" in above line, I'm not sure why role_ids is taken instead of 'role' attribute

Employee.rb

class Employee < ActiveRecord::Base

attr_accessible :name, :role, :reports_to, :role_ids
validates :name, :presence => true, :length => { :maximum => 20 }

belongs_to :company
has_and_belongs_to_many :roles
end

Role.rb

class Role < ActiveRecord::Base
    attr_accessible :title  
     has_and_belongs_to_many :employees
end

employees_controller.rb

def create
    @employee = User.new(params[:user])
    @role = @employee.roles.build(params[:role]) unless @user.roles.build(params[:role]).blank? 
    respond_to do |format|
      if @employee.save
        format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
        format.json { render json: @employee, status: :created, location: @employee }
      else
        format.html { render action: "new" }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

_form.html.erb

    <%= simple_form_for(@employee, :html => {:class => 'form-horizontal' }) do |f| %>
<fieldset>
    <legend><%= controller.action_name.capitalize %> Employee</legend>  
 <div class="control-group">
 <%= f.label :name, :class => 'control-label' %>
 <div class="controls">
 <%= f.input :name, :label => false %>
  </div>
</div>
<div class="control-group">
 <%= f.label :role, :class => 'control-label' %>
<div class="controls">
 <%= f.association :roles, :collection => Role.all(:order => 'title'), :label_method => :id, :prompt => "Assign role", :label => false %>
    </div>
  </div>
<div class="control-group">
 <%= f.label :reports_to, :class => 'control-label' %>
<div class="controls">
  <%= f.input :reports_to, :collection => [ "Sam", "Adam", "Smith"], :prompt => "Select supervisor", :label => false %>
  </div>
  </div>
  <div class="form-actions">  
<%= f.button :submit %>
</div>
 </fieldset>
<% end %>
Sudhakar
  • 2,904
  • 8
  • 33
  • 47

1 Answers1

0

You may want to consider reading the Rails Guide on nested resources.

If you want to make things even easier on yourself, I highly recommend looking into Jose Valim's Inherited Resources gem that takes care of a lot of this boilerplate nastiness for you.

R Milushev
  • 4,295
  • 3
  • 27
  • 35
  • Thank you for your response @Qumara. I'll read up on it Meanwhile if you have a clue to fix it please do. Thanks again! – Sudhakar Dec 06 '12 at 07:43
  • It seems that first you should create a join table for storing all the combination for roles_id and employes_id . (Here you can read about it):http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many . I see in your hash there is "role_ids"=>["", "4"] , it expects one more value , which you should supply . – R Milushev Dec 06 '12 at 08:18
  • I have created a join table roles_users. I can even see the roles listing in the dropdown, but the selected value is not getting passed after submit. I'm suspecting that create method (updated in the question) is the culprit but after tons of googling I still cannot figure out why the value is not passed to the model – Sudhakar Dec 06 '12 at 09:02
  • corection: I meant employees_roles table – Sudhakar Dec 06 '12 at 09:10