I have two models jobs and clients.
A user can simple create a client and then assign them a number of jobs.
Here’s my models for both.
job.rb
class Job < ActiveRecord::Base
has_and_belongs_to_many :clients
end
client.rb
class Client < ActiveRecord::Base
has_and_belongs_to_many :jobs
end
My form for creating a new job looks like this:
<%= simple_form_for :job do |f| %>
<%= f.input :name %>
<%= <%= collection_select(:job, :client_ids, Client.all, :id, :name, {:include_blank => 'None'}, { :multiple => true }) %>%>
<%= f.button :submit %>
<% end %>
So as you can see there is a drop down box on the form which contains all of the clients.
When try to save it however, I recieve this messed:
ActiveRecord::UnknownAttributeError in JobsController#create
unknown attribute: client_id
Application Trace | Framework Trace | Full Trace
app/controllers/jobs_controller.rb:22:in `new'
app/controllers/jobs_controller.rb:22:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"0ZVYpM9vTgY+BI55Y9yJDwCJwrwSgGL9xjHq8dz5OBE=",
"job"=>{"name"=>"Sample Monthly",
"client_id"=>"1"},
"commit"=>"Save Job"}
My job controller is quite basic and looks like this:
class JobsController < ApplicationController
def index
@jobs = Job.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @job }
end
end
def new
@jobs = Job.new
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @job }
end
end
def create
@jobs = Job.new(params[:job])
respond_to do |format|
if @jobs.save
format.html { redirect_to @jobs, notice: 'Job was successfully created.' }
format.json { render json: @jobs, status: :created, location: @jobs }
else
format.html { render action: "new" }
format.json { render json: @jobs.errors, status: :unprocessable_entity }
end
end
end
def show
@jobs = Job.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @jobs }
end
end
end
I have a junction table setup with both job_id and client_id as a integer value in them.
So I think its just a case of defining them in my controller under the new and create action like the error message suggests.
This is my first Rails app though sand im not quite sure how.
Any help would be greatly appreciated!
Problem might be in your form. Try to replace this
with
so the form will look like this