我有一个嵌套的表单,并且无法使创建和更新功能正常工作。
我认为发生的事情是首先创建嵌套对象,并导致错误。
尝试运行创建操作时出现此错误:
ActiveRecord::RecordNotFound in ApplicationsController#create
Couldn't find Answer with ID=5 for Application with ID=
Rails.root: /Users/stepan/Desktop/Sites/atlas
Application Trace | Framework Trace | Full Trace
app/controllers/applications_controller.rb:22:in `create'
这只发生在我允许了答案模型的:id参数之后。我这样做的原因是为了使更新操作成为可能。
这就是我的控制器的样子 - >
class ApplicationsController < ApplicationController
def new
@job = Job.find(params[:job_id])
@user = current_user
@application = Application.new()
@job.questions.count.times do
@application.answers.build
end
end
def create
@user = current_user
@job = Job.find(params[:job_id])
@application = Application.new(application_params)
@application.job_id = @job.id
if @application.save
redirect_to root_url, :notice => "You have now applied!"
else
render :action => 'new'
end
end
def edit
@job = Job.find(params[:job_id])
@user = current_user
@application = Application.find(params[:id])
@answers = []
@job.questions.each do |question|
@application.answers.each do |answer|
@answers << answer if answer.question_id == question.id
end
end
end
def update
@job = Job.find(params[:job_id])
@user = current_user
@application = Application.find(params[:id])
if @application.update_attributes(application_params)
redirect_to root_url, :notice => "You have updated your application!"
else
render :action => 'new'
end
end
def destroy
Application.find(params[:id]).destroy
flash[:success] = "Application Deleted."
redirect_to root_url
end
def show
@job = Job.find(params[:job_id])
@user = current_user
@application = Application.find(params[:id])
@answers = []
@job.questions.each do |question|
@application.answers.each do |answer|
@answers << answer if answer.question_id == question.id
end
end
end
private
def application_params
params.require(:application).permit(:id, :job_id, :user_id, answers_attributes:[:content, :question_id, :id]).merge(user_id: current_user.id)
end
end
这是表格:
<% provide(:title, " Apply to this job") %>
<div class="row">
<div class="span6">
<h2> Job: <%= @job.job_title %></h2>
<p> <%= @job.job_summary %> </p>
</div>
<div class="span6">
<h2> Applicant: <%= @user.name %></h2>
</div>
<div class="span12">
<h3>You're almost done! Answer the questions below and you'll be applied to the job.</h3>
</div>
</div>
<%= form_for [@job, @application] do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<% @job.questions.each_with_index do |question| %>
<%= f.fields_for :answers, question do |question_field| %>
<%= question_field.label :content, question.content %>
<%= question_field.text_area :content, :value => "" %>
<%= question_field.hidden_field :question_id, :value => question.id %>
<% end %>
<% end %>
<%= f.submit "Submit the application", class: "button" %>
<% end %>
这是我的应用程序模型:
# == Schema Information
#
# Table name: applications
#
# id :integer not null, primary key
# user_id :integer
# job_id :integer
# created_at :datetime
# updated_at :datetime
#
class Application < ActiveRecord::Base
belongs_to :job
belongs_to :user
validates :job_id, presence: true
validates :user_id, presence: true
has_many :answers
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
这是我的答案模式:
# == Schema Information
#
# Table name: answers
#
# id :integer not null, primary key
# application_id :integer
# question_id :integer
# created_at :datetime
# updated_at :datetime
# content :string(255)
#
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :application
validates :content, presence: true
end
这里有类似的问题 - &gt; Rails 4 Nested Attributes Unpermitted Parameters,但我不太明白答案是做什么的。
非常感谢!
答案 0 :(得分:1)
您可能会受益于以下问题:
应用创建
直言不讳地说,我认为您正在尝试使用应用程序create
函数
Rails(和所有MVC)中有一个名为fat model skinny controller的原则,这意味着你应该尽可能多地将动作放入模型中。控制器实际上只需要引导逻辑(根据模型响应调用各自的方法)。这就是我要做的事情:
#app/controllers/applications_controller.rb
before_filter :set_user
def new
job_id = params[:job_id]
@application = Application.build(job_id)
end
def create
@application = Application.new(application_params)
@application.save
redirect_to root_url, :notice => "You have now applied!"
end
private
def set_user
@user = current_user
end
def application_params
params.require(:application).permit(:job_id, :user_id, answers_attributes:[:question_id, :content]).merge(user_id: current_user.id, job_id: params[:job_id])
end
#app/models/application.rb
Class Application < ActiveRecord::Base
#Associations here
accepts_nested_attributes_for :answers
validates :title, :other_application_vars,
presence: true #-> shows errors if validation fails (no need for "if @application.save" logic.... needs testing)
def self.build(job_id) #-> class method -- not sure about passing arguments
application = self.new
job = Job.find(job.id)
job.questions.count.times do
application.answers.build
end
application
end
end
#app/models/answer.rb
Class Answer < ActiveRecord::Base
validates :question_id, :content,
presence: true
end
强大的参数
您提到只有在您发送id
参数时才会执行此操作。我在这里没有丰富的经验,但我发现你不需要通过强大的参数发送:id
参数。这可能是造成这个问题的原因 - 你没有设置id
param,因此它变得混乱了
如果您采用conventional方式执行此操作,则不应出现此错误