ruby-on-rails - 参数不会传入子类

标签 ruby-on-rails ruby ruby-on-rails-3

当我尝试创建工作时,它会显示“company must exist”,c_name 和 company_id 不会传入 views/jobs/_form

迁移

class CreateJobs < ActiveRecord::Migration[5.1]
  def change
    create_table :jobs do |t|
      t.string :title
      t.text :description
      t.string :c_name
      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

模型

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

作业 Controller

def show
end

def new
  @job = current_user.jobs.build       
end

def create
  job_attrs = jobs_params.except(:c_name)
  job_attrs[:c_name] = Company.find_by(id: jobs_params[:c_name])
  @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, 
            :c_name, 
            :category_id, 
            :image, 
            :jobclosedate, 
            :company_id)
end

工作/_表格

<%= 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 :c_name, label: "Your Company", input_html: { class: "form-control" } %>
  <%= f.collection_select :category_id, Category.all, :id, :name, {promt: "Choose a category" } %>

当我尝试创建工作时,它会返回“公司必须存在”验证错误,c_namecompany_id 不会传入 views/jobs/_form.

你能帮我解决一下吗?似乎创建操作有问题。我是 Ruby 新手,请解释我如何解决这个问题。

最佳答案

我看到您不是从选择中挑选公司,而是只是写下名称。在您的 Controller 中,您正在按名称寻找公司。这种方法不是很好,因为用户可以写出不存在或拼写错误的公司名称。您可以使用选择标签并传递 company_id。

正确的做法是:

迁移:

class CreateJobs < ActiveRecord::Migration[5.1]
  def change
    create_table :jobs do |t|
      t.string :title
      t.text :description
      #t.string :c_name REMOVE THIS. YOU DON'T NEED THE COMPANY NAME HERE.
      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

模型

class User < ApplicationRecord
  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

作业 Controller

def new
  @job = current_user.jobs.build       
end

def create
  @job = current_user.jobs.build(job_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, 
        :category_id, 
        :image, 
        :jobclosedate, 
        :company_id)
end

工作/_表格

<%= simple_form_for(@job,validation:true ,html: { multipart: 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" } %>
  <!-- Removed the c_name field -->
  <%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Choose a category" } %>
  <%= f.collection_select :company_id, current_user.companies.all, :id, :name, {prompt: "Choose a company" } %>
<% end %>

注意:您的工作属于用户。它也属于一家公司。公司属于用户。所以job中的用户可能不需要(可以通过job.company.user访问(除非job.user是其他概念,和公司用户不一样)

以前的方法

无论如何,如果你想在 View 中继续使用公司名称,你必须在 Controller 中进行更改。您用来按名称搜索公司的代码是错误的。您需要获取 company_id 以放入参数(以及 Job 记录中),但您只是添加了整个公司记录。

代替

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

你应该写

company = current_user.companies.find_by(name: jobs_params[:c_name])
if company
  job_attrs[:company_id] = company.id
else
  # Error. Do not create job...
end

最后,您的 Job 表中不需要公司名称(只需删除 c_name)。如果您仍然想在 View 中使用 c_name 来获取公司,只需在模型中定义一个属性(但不要将其保存在表中)。

attr_accessor :c_name;

关于ruby-on-rails - 参数不会传入子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48670646/

相关文章:

ruby-on-rails - 是否可以将外部路由文件包含到主 routes.rb 文件中?

ruby-on-rails - Rails 将包含日期时间的字符串转换为日期

ruby-on-rails - Rails 中的 SHA1 哈希

ruby-on-rails - 尝试获取哈希值时的 `try` 方法

ruby-on-rails - rails 中的身份验证系统

ruby-on-rails - 可以使用扣币一次性付款吗?

ruby-on-rails - 如何防止 Rails 中的浏览器页面缓存

ruby-on-rails - rails : each in random order

ruby-on-rails - 如何在Rails功能测试中调用调试器?

ruby-on-rails - 引用 has_many (Rails) 中的实例