[英]Rails 4, saving attributes nested
I have a model Job. 我有一个模特乔布。 A job require many skills.
工作需要很多技能。 But when I'm trying to save my job it fails.
但是,当我试图保存我的工作时,它失败了。 I'm not sure I understand what I'm doing wrong.
我不确定我了解我在做什么错。 My models:
我的模特:
class Skill < ActiveRecord::Base
has_many :job_skills
has_many :jobs, through: :job_skills
end
class JobSkill < ActiveRecord::Base
belongs_to :skill
belongs_to :job
end
class Job < ActiveRecord::Base
has_many :job_skills, :inverse_of => :job
has_many :skills, through: :job_skills
accepts_nested_attributes_for :job_skills
end
My view: 我的观点:
<%= form_for @job do |f| %>
<div class="row">
<div class="col-md-8">
<h4>General informations</h4>
<br />
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, :autofocus => true, class:'form-control' %>
</div><br />
<%= f.fields_for :job_skills do |s| %>
<%= s.text_field :id %>
<% end %>
</div>
</div>
<div class="submit" style="position:relative;">
<%= f.submit "Save", class: 'button button-small' %>
</div>
<% end %>
My controller: 我的控制器:
class JobsController < ApplicationController
before_filter :authenticate_user!, :has_company?, :except => [:index, :show]
def create
@job = Job.new(job_params)
@job.company_id = current_user.company_id
@job.user_id = current_user.id
if @job.save
flash[:notice] = "This job offer has been saved."
return redirect_to job_path(@job)
else
flash[:error] = @job.errors.full_messages.to_sentence
render action: :new
end
end
def new
if current_user.company.present?
@job = Job.new(email:current_user.email)
@job.job_skill.build
else
flash[:error] = "You need to create a company before posting a job"
return redirect_to new_company_path()
end
end
private
def job_params
params.require(:job).permit(:status, :title, :description, :remote ,:job_type, :visa_sponsor, :email, :salary_max, :salary_min, :country, :state, :city, job_skill_attributes: [:id])
end
end
So, I'm not sure what I'm doing wrong, when I'm trying to save I get the following error: 因此,我不确定自己在做什么错,当我尝试保存时出现以下错误:
@job = Job.new(job_params)
@job = Job.new(job_params)
ActiveRecord::RecordNotFound Exception:
ActiveRecord :: RecordNotFound异常:
Couldn't find Skill with ID=4 for Job with ID= nil
找不到ID = nil的Job的ID = 4的技能
Your pieces of code are a bit confusing for me. 您的代码片段让我有些困惑。 It seems, that you want to create a job and define, what skills are needed for this job.
看来,您想创建一个工作并定义该工作需要哪些技能。 Why do you need nested attributes?
为什么需要嵌套属性? Normally, you
通常你
either edit a list of all the skills, that are probably needed for a job and then assign the propper skills to that job, than you have a has_and_belongs_to_many
relationship and can use form helpers for collections. 要么编辑一份工作可能需要的所有技能的列表,然后为该工作分配适当的技能,否则您将拥有
has_and_belongs_to_many
关系,并且可以使用表单助手进行收藏。 In this case, you don't need a model JobSkill
(but a table jobs_skills
to store the relationship and is handles transparently by Rails) 在这种情况下,您不需要模型
JobSkill
(但是需要一个表jobs_skills
来存储关系,并且由Rails透明地进行处理)
or add random skills to a job, then your job has_may :skills
and every skill belongs to exactly one job. 或在工作中添加随机技能,则您的工作
has_may :skills
而每个技能恰好属于一项工作。 Here you can use nested attributes. 在这里您可以使用嵌套属性。 And then you need a way to add nested skill instances ie with cocoon .
然后,您需要一种添加嵌套技能实例的方法,例如使用cocoon 。 Again, you don't need a model
JobSkill
. 同样,您不需要模型
JobSkill
。
Which one is your usecase, so I can explain it in more detail. 哪一个是您的用例,所以我可以更详细地解释它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.