在 Rails 4 个 App 中按 ID 查找对象
Find object by ID in Rails 4 App
我使用 Wicked. I've specifically followed this guide 在我的 rails 4 应用程序中构建了一个向导,唯一的区别是我使用嵌套资源执行此操作。我在执行控制器操作时遇到困难,这些操作应该使用其 ID 找到表单正在构建的对象。
应用程序的当前流程是用户单击创建对象的按钮,然后被重定向到向导的第一步,该向导通过一系列步骤更新对象。问题是我没有通过参数传递对象的 ID,因为我在 ID 字段中传递了向导的步骤。
创建对象的按钮
<%= link_to business_loan_application_path(@user), method: :post, :class => "btn btn-success" do %><% end
%>
LoanApplication Controller
def create
@user = current_user
@loanapplication = @user.loan_applications.new
@loanapplication.save(validate: false)
redirect_to business_loan_application_wizard_path(@user.id,LoanApplication.form_steps.first)
end
我需要以某种方式将 :loan_application_id 传递给 business_loan_application_wizard_path 参数。
错误
Couldn't find LoanApplication without an ID
LoanApplication belongs_to 通过 has_many 关系的业务。
{"business_id"=>"55", "id"=>"business_details"}
business_details 是向导中的第一步。这些是我第一次通过带有放置请求的按钮创建对象时传递的参数。
向导控制器
class WizardController < ApplicationController
include Wicked::Wizard
before_filter :authenticate_user!
before_filter :require_business
steps *LoanApplication.form_steps
def show
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
render_wizard
end
def update
@business = current_user
@loanapplication = @business.loan_applications.last
@loanapplication.update_attributes(loan_application_params(step))
render_wizard @loanapplication
end
end
this question中的讨论帮助解决了这个问题。
已更改
resources :wizard
到
scope "loan_application/:loan_application_id" do
resources :wizard
end
在我的 routes.rb 文件中。
我的向导控制器现在如下:
def show
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
render_wizard
end
def update
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
@loanapplication.update_attributes(loan_application_params(step))
render_wizard @loanapplication
end
最后,贷款申请控制器重定向 link 已更改:
def create
@user = current_user
@loanapplication = @user.loan_applications.new
@loanapplication.save(validate: false)
redirect_to business_loan_application_wizard_path(@user.id, LoanApplication.form_steps.first, :loan_application_id => @loanapplication.id)
end
我使用 Wicked. I've specifically followed this guide 在我的 rails 4 应用程序中构建了一个向导,唯一的区别是我使用嵌套资源执行此操作。我在执行控制器操作时遇到困难,这些操作应该使用其 ID 找到表单正在构建的对象。
应用程序的当前流程是用户单击创建对象的按钮,然后被重定向到向导的第一步,该向导通过一系列步骤更新对象。问题是我没有通过参数传递对象的 ID,因为我在 ID 字段中传递了向导的步骤。
创建对象的按钮
<%= link_to business_loan_application_path(@user), method: :post, :class => "btn btn-success" do %><% end
%>
LoanApplication Controller
def create
@user = current_user
@loanapplication = @user.loan_applications.new
@loanapplication.save(validate: false)
redirect_to business_loan_application_wizard_path(@user.id,LoanApplication.form_steps.first)
end
我需要以某种方式将 :loan_application_id 传递给 business_loan_application_wizard_path 参数。
错误
Couldn't find LoanApplication without an ID
LoanApplication belongs_to 通过 has_many 关系的业务。
{"business_id"=>"55", "id"=>"business_details"}
business_details 是向导中的第一步。这些是我第一次通过带有放置请求的按钮创建对象时传递的参数。
向导控制器
class WizardController < ApplicationController
include Wicked::Wizard
before_filter :authenticate_user!
before_filter :require_business
steps *LoanApplication.form_steps
def show
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
render_wizard
end
def update
@business = current_user
@loanapplication = @business.loan_applications.last
@loanapplication.update_attributes(loan_application_params(step))
render_wizard @loanapplication
end
end
this question中的讨论帮助解决了这个问题。
已更改
resources :wizard
到
scope "loan_application/:loan_application_id" do
resources :wizard
end
在我的 routes.rb 文件中。
我的向导控制器现在如下:
def show
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
render_wizard
end
def update
@business = current_user
@loanapplication = LoanApplication.find(params[:loan_application_id])
@loanapplication.update_attributes(loan_application_params(step))
render_wizard @loanapplication
end
最后,贷款申请控制器重定向 link 已更改:
def create
@user = current_user
@loanapplication = @user.loan_applications.new
@loanapplication.save(validate: false)
redirect_to business_loan_application_wizard_path(@user.id, LoanApplication.form_steps.first, :loan_application_id => @loanapplication.id)
end