我的应用程序的简要概述。
这是非常基本的,因为用户首先在一个页面上创建一个集合 A 客户端,然后使用另一个页面来创建和分配作业给用户。
我的客户模型和视图按预期工作,但我无法链接我的工作模型。
这是我的工作模型。
class Client < ActiveRecord::Base 
  has_and_belongs_to_many :jobs
end
class Job < ActiveRecord::Base
  has_and_belongs_to_many :clients
end
这也是我的客户控制器。
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
在我的表单中,我有两个字段。一个是作业名称,另一个是数据库中列出的所有客户端的下拉列表。
但是,当填写此内容并按保存时,我会收到以下错误。
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"}
我有一个连接表设置clients_jobs也称为..
class AddClientsJobsTable < ActiveRecord::Migration
  def up
    create_table :clients_jobs, :id => false do |t|
      t.belongs_to :job, :client
      t.integer :client_id
      t.integer :job_id
  end
end
  def down
    drop_table :clients_jobs
  end
end
我想我需要声明client_id
某处,但这是我的第一个 Rails 应用程序,我不确定在哪里。
任何帮助将不胜感激。
编辑:这是我的工作表格。
<%= simple_form_for :job do |f| %>
  <%= f.input :name %>
  <%= select("job", "client_id", Client.all.collect {|c| [ c.name, c.id ] }, {:include_blank => 'None'})%>
  <%= f.button :submit %>
<% end %>