我想在" view / jobs / _form.html.erb"中自动填充公司名称。

时间:2018-02-07 07:25:58

标签: ruby-on-rails

公司名称应自动填写在工作视图表单中

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.integer :company_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_attrs = jobs_params.except(:company)
    job_attrs[:company] = Company.find_by(id: 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

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

在views / jobs / _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" }%>

公司名称应自动填充在views / jobs / _form.html.erb中,它不会自动填充,当我尝试创建作业时,它不会自动获取公司名称,请你帮忙我找出这个问题

3 个答案:

答案 0 :(得分:0)

尝试更改:

job_attrs[:company] = Company.find_by(id: jobs_params[:company])

job_attrs[:company] = Company.find_by(id: jobs_params[:company_id])

答案 1 :(得分:0)

你有什么是嵌套表格。请查看simple_form Associations

它应该如何(可能需要自己调整)

views/jobs/_form

= f.simple_fields_for :company do |company_fields|
  = company_fields.input :c_name, label: "Your Company", input_html: { class: "form-control" }

JobsController

 def create       
   @job = current_user.jobs.build(job_params)
   if @job.save
   .....
 end

 def jobs_params
   params.require(:job).permit(:title, :description, :company, :category_id, :image,:jobclosedate,company_attributes: [:id, :c_name])
 end

Job Model

 accepts_nested_attributes_for :company

答案 2 :(得分:0)

如果您需要在公司创建一个公司,该公司由该用户创建的公司,用户需要创建一个工作,那么您需要collection_of_select下拉列表,以便更容易选择公司,如下所示

<%= f.input :company_id, :collection: current_user.companies, label_method: :c_name, value_method: :id, label: "Company", include_blank: false %>

如果current_user创建一个或多个公司,那么所有公司都将填入下拉表单。

您需要将company_id添加到作业创建强参数。