将嵌套参数从重定向中的创建传递到嵌套控制器的显示

Passing nested params from create in redirect to show of nested controller

我的架构如下:

我有一个 simple_form_for 使用 cocoon 从 Company 控制器中的 create 操作创建 Company & Quote & Employees,所有 objects 都由 create 方法创建得很好。但是在保存这些 objects 时,我试图重定向到由 Companies#create 创建的报价的 Quotes#show 但我无法将 quote_id 转到 Quotes#show。

你能帮我理解如何获得正确的参数以成功重定向吗?谢谢。

companies.rb

class CompaniesController < ApplicationController
    before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]

 def new
    @company = Company.new
    @company.quotes.build
    @company.employees.build
  end

  def create
    @company = current_user.companies.new(company_params)
    if @company.save
      redirect_to company_quote_url(@company.id), notice: 'Quote request created'
    else
      render :new
    end
  end

private

  def company_params
    params.require(:company).permit(:co_name, :co_number, :postcode, :industry,
    :quotes_attributes =>       [:id, :lives_overseas, :payment_frequency],
    :employees_attributes =>    [:id, :first_name, :last_name, :email, :gender, :date_of_birth, :salary, :_destroy] )
  end
end

quotes.rb

class QuotesController < ApplicationController
  def show
    @quote = @company.quotes.find(params)
  end
end

路线

           quotes_show GET    /quotes/show(.:format)                         quotes#show
            quotes_index GET    /quotes/index(.:format)                        quotes#index
        new_user_session GET    /users/sign_in(.:format)                       devise/sessions#new
            user_session POST   /users/sign_in(.:format)                       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                      devise/sessions#destroy
       new_user_password GET    /users/password/new(.:format)                  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                 devise/passwords#edit
           user_password PATCH  /users/password(.:format)                      devise/passwords#update
                         PUT    /users/password(.:format)                      devise/passwords#update
                         POST   /users/password(.:format)                      devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)                        devise/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)                       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                          devise/registrations#edit
       user_registration PATCH  /users(.:format)                               devise/registrations#update
                         PUT    /users(.:format)                               devise/registrations#update
                         DELETE /users(.:format)                               devise/registrations#destroy
                         POST   /users(.:format)                               devise/registrations#create
          company_quotes GET    /companies/:company_id/quotes(.:format)        quotes#index
           company_quote GET    /companies/:company_id/quotes/:id(.:format)    quotes#show
       company_employees GET    /companies/:company_id/employees(.:format)     employees#index
        company_employee GET    /companies/:company_id/employees/:id(.:format) employees#show
               companies GET    /companies(.:format)                           companies#index
                         POST   /companies(.:format)                           companies#create
             new_company GET    /companies/new(.:format)                       companies#new
            edit_company GET    /companies/:id/edit(.:format)                  companies#edit
                 company GET    /companies/:id(.:format)                       companies#show
                         PATCH  /companies/:id(.:format)                       companies#update
                         PUT    /companies/:id(.:format)                       companies#update
                         DELETE /companies/:id(.:format)                       companies#destroy
                    root GET    /                                              companies#new

错误信息

No route matches {:action=>"show", :company_id=>65, :controller=>"quotes"} missing required keys: [:id]

我看不出如何获得从 Companies#create 到 Quotes#show 的 quote_id 路线。当我 运行 重定向时; redirect_to company_quote_url(company_params) 错误显示 header 参数是这样传递的,即这是可用的;

No route matches {:action=>"show", "co_name"=>"acme1", "co_number"=>"12345678", :controller=>"quotes",

"employees_attributes"=>{"0"=>{"first_name"=>"brian", "last_name"=>"blessed", "email"=>"brian@test.com", "gender"=>"m", "date_of_birth(1i)"=>"2001", "date_of_birth(2i)"=>"6", "date_of_birth(3i)"=>"28", "salary"=>"10000", "_destroy"=>"false"}}, "industry"=>"financial_services", "postcode"=>"al8 8ba",

"quotes_attributes"=>{"0"=>{"lives_overseas"=>"true", "payment_frequency"=>"annually"}}} missing required keys: [:company_id, :id]

我玩过但失败了;

但我似乎无法正确处理,请有人帮忙。谢谢

由于 company has_many quotes,您应该跟踪该公司的最新报价 并重定向到引用的显示视图。

def create
  @company = current_user.companies.new(company_params)

  if @company.save
    @quote = @company.quotes.last
    redirect_to company_quote_url(@company,@quote), notice: 'Quote request created'
  else
    render :new
  end
end

last将在created_at

的帮助下为您提供最新记录

另外你应该像下面那样调整你的 quotes#show 否则它会出错。

def show
  @company = Company.find(params[:company_id])
  @quote = @company.quotes.find(params[:id])
end