从重定向中的create传递嵌套参数以显示嵌套控制器

时间:2017-06-29 07:48:06

标签: ruby-on-rails nested-attributes rails-routing

我有一个类似的架构:

  • 公司属于用户(设计)
  • 报价属于公司
  • 员工属于公司

我有一个simple_form_for使用cocoon创建公司&引用&来自公司控制器中的创建操作的员工,通过create方法创建的所有对象都很好。但是对于这些对象的.save,我试图重定向到#Quote,显示由公司创建的报价#infate,但是我无法将quote_id转到报价#show。

您能帮助我了解如何将正确的参数转换为成功重定向吗?感谢。

companies.rb

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

quotes.rb

           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]

错误消息

redirect_to company_quote_url(company_params)

我无法看到如何通过公司#create over the Quotes#show获得quote_id路线。当我运行重定向时; 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] 错误显示标题参数像这样传递,即这是可用的;

                          hash
                    vvvvvvvvvvvvvvvv
$hash = '1234567890:abcdef1234567890'
         ^^^^^^^^^^
         timestamp

我已经演奏过,并且因为不同的变奏而失败;

  • (PARAMS [:公司] [:quote_attributes [:ID]])
  • (@ company.quote.id)

但我似乎无法做到正确,任何人都可以帮忙。感谢

1 个答案:

答案 0 :(得分:1)

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

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