JobsController#creat中的ActiveRecord :: AssociationTypeMismatch并希望自动填充公司名称

时间:2018-02-06 16:13:21

标签: ruby-on-rails

当我创建一个Job时,它会在Job类中使用belongs_to :company时显示此错误。我想在_form输入中自动填充公司名称,我该怎么做,(我想将它显示为view / jobs / _form.html.erb)

为什么我收到此错误?

ActiveRecord::AssociationTypeMismatch in JobsController#create
Company(#153084460) expected, got "VIrtusa Corperation-" which is an instance of String(#9740380)

    # Migrations
    class CreateJobs < ActiveRecord::Migration[5.1]
      def change
        create_table :jobs do |t|
          t.string :title
          t.text :description
          t.string :company
          t.integer :user_id
          t.timestamps
        end
      end
    end

    class CreateCompanies < ActiveRecord::Migration[5.1]
      def change
        create_table :companies do |t|
          t.string :c_name
          t.text :c_description
          t.integer:user_id
          t.timestamps
        end
      end
    end

    # Models
    class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      has_many :companies
      has_many :jobs
    end

    class Job < ApplicationRecord
        belongs_to :user
        belongs_to :category
        belongs_to :company
    end

    class Company < ApplicationRecord
        belongs_to:user
        has_many:jobs

    end

    # Jobs controller
    def show
            end

            def new

                   @job = current_user.jobs.build       
            end

    def create
        @job = current_user.jobs.build(jobs_params)

          if @job.save
            flash[:success]= "success"
            redirect_to @job

            else

            flash[:error]=@job.errors.full_messages
            render "new"
            end
        end

    def jobs_params
                params.require(:job).permit(:title, :description, :company, :category_id, :image,:jobclosedate)
    end

观看/工作/ _form文件

<%= simple_form_for(@job,validation:true ,html: { mutlipart: true, class: 'form-horizontal'}) do |f| %>
    <%= f.input :title, label: "Job Title", input_html: { class: "form-control"}%>
    <%= f.input :description, label: "Job Description", input_html: { class: "form-control" }%>
    <%= f.input :company, label: "Your Company", input_html: { class: "form-control" }%>
    <%= f.collection_select :category_id,Category.all, :id, :name, {promt: "Choose a category" }%>

<% end %>

2 个答案:

答案 0 :(得分:0)

被接受的参数应该是company_id而不是公司。

您的迁移不包括公司和工作之间的关系。在工作中,您应该引用公司(company_id而不是名称)

您必须传递company_id(似乎您传递了名称)

答案 1 :(得分:0)

我认为发生此错误是因为company中的jobs_params是一个字符串。我假设这个属性的含义是公司名称。有两种解决方案可供解决:

1 - 使用company_id代替company向控制器请求

2 - 在create

中查找相应的公司
def create
  job_attrs = jobs_params.except(:company)
  job_attrs[:company] = Company.find_by(c_name: jobs_params[:company])

  @job = current_user.jobs.build(job_attrs)

  if @job.save
    flash[:success]= "success"
    redirect_to @job

  else

    flash[:error]=@job.errors.full_messages
    render "new"
  end
end