我有一个错误,指出:找不到没有ID的作业。错误消息引用我的应用程序模型(application.rb)中的以下编码
job = Job.find(@job_id)
我的终端登录状态:
在2014-06-12 13:37:42为127.0.0.1开始POST“/ applications” +0100由ApplicationsController处理#create as HTML参数:{“utf8”=>“✓”, “authenticity_token”=> “中r36zBqgzp48ssfbOsl51EoKbxClr5oaogiqU9pwh / HE =” “application”=> {“question”=>“我准备雇用”,“questiona”=>“我 拥有并住在这家酒店“,”jobstartdate_id“=>”“,”邮政编码“=>”“, “description”=>“”},“commit”=>“下一步”} 完成404未找到1ms
ActiveRecord :: RecordNotFound(无法找到没有ID的作业):
app / models / application.rb:22:在steps'
current_step'中 app / models / application.rb:44:in
app/models/application.rb:18:inlast_step?'
create'
app/controllers/applications_controller.rb:32:in
应用程序/控制器/ applications_controller.rb
def index
@applications = Application.all
end
def show
@application = Application.find(params[:id])
end
def new
session[:application_params] ||= {}
@application = Application.new(session[:application_params])
@application.current_step = session[:application_step]
@application.job_id = params[:application][:job_id]
end
def create
session[:application_params].deep_merge!(params[:application]) if params[:application]
@application = Application.new(session[:application_params])
@application.current_step = session[:application_step]
if params[:previous_button]
@application.previous_step
elsif @application.last_step?
@application.save
else
@application.next_step
end
session[:application_step] = @application.current_step
if @application.new_record?
render :new
else
session[:application_step] = session[:application_params] = nil
flash[:notice] = "Application saved"
redirect_to @application
end
end
应用程序/视图/应用/ _form.html.erb
<%= simple_form_for(@application) do |f| %>
<% if @application.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@application.errors.count, "error") %> prohibited this application from being saved:</h2>
<ul>
<% @application.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render "#{@application.current_step}_step", :f => f %>
<div class="actions">
<%= f.submit "Back", :name => "previous_button" unless @application.first_step? %>
</div>
<div class="actions">
<%= f.submit "Next" %>
</div>
<p><%= link_to "Back to application list", applications_path %></p>
<% end %>
app / models / application.rb
class Application < ActiveRecord::Base
attr_accessible :firstname, :lastname, :phone, :email, :question, :questiona, :questionb, :postcode, :description, :job_id, :trade_id, :budget_id, :jobstartdate_id
attr_writer :current_step, :job_id
belongs_to :job
belongs_to :trade
belongs_to :budget
belongs_to :jobstartdate
# validates :firstname, presence: true
# validates :lastname, presence: true
# validates :phone, presence: true
# validates :email, presence: true
def current_step
@current_step || steps.first
end
def steps
job = Job.find(@job_id)
if job.name == "Digital Home Network"
%w[digital_Home_Network budget contact]
else
%w[bathroomInstallation budget contact]
end
end
def next_step
self.current_step = steps[steps.index(current_step)+1]
end
def previous_step
self.current_step = steps[steps.index(current_step)-1]
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
end
答案 0 :(得分:1)
您必须访问job_id:
self.job_id
只要它是模型中的属性,而不是初始化变量。
我不同意这种模型的更新。如果您希望在应用程序使用嵌套参数的同时创建作业,请在保存调用之前执行。您正在丢失工作验证,错误和消息,这使得工作流程变得非常复杂。
答案 1 :(得分:0)
感谢大家的回复。问题现在已经解决了。
在模型(application.rb)中,我不得不添加到创建操作:
@ application.job_id = params [:application] [:job_id]
def create
session[:application_params].deep_merge!(params[:application]) if params[:application]
@application = Application.new(session[:application_params])
@application.current_step = session[:application_step]
@application.job_id = params[:application][:job_id]
if params[:previous_button]
@application.previous_step
elsif @application.last_step?
@application.save
else
@application.next_step
end
session[:application_step] = @application.current_step
if @application.new_record?
render :new
else
session[:application_step] = session[:application_params] = nil
flash[:notice] = "Application saved"
redirect_to @application
end
end
然后在_form.html.erb(views / application / _form.html.erb)中: 我不得不添加一个隐藏的字段:
<%= simple_form_for(@application) do |f| %>
<% if @application.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@application.errors.count, "error") %> prohibited this application from being saved:</h2>
<ul>
<% @application.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :job_id, {value: @application.job_id} %>
<%= render "#{@application.current_step}_step", :f => f %>
<div class="actions">
<%= f.submit "Back", :name => "previous_button" unless @application.first_step? %>
</div>
<div class="actions">
<%= f.submit "Next" %>
</div>
<p><%= link_to "Back to application list", applications_path %></p>
<% end %>