【问题标题】:Passing nested params from create in redirect to show of nested controller将嵌套参数从创建重定向传递到嵌套控制器的显示
【发布时间】:2017-06-29 07:48:05
【问题描述】:

我有一个类似的架构:

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

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

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

公司.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) 错误显示标题参数是这样传递的,即这是可用的;

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]

我玩过不同的变体,但都失败了;

  • (params[:company][:quote_attributes[:id]])
  • (@company.quote.id)

但我似乎无法正确解决问题,任何人都可以帮忙。谢谢

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • @jbk 我的错误,company_quote_url 应该是 quote_url 或使用 company_quote_url(@company,@quote)
    • @jbk 使用company_quote_url(@company,@quote) 并在quote#show 方法中将@company 定义为Company.find(params[:company_id])
    • 感谢 Pavan,现在一切正常 ??。我想知道有没有更好的方法来做这件事,似乎这种来回传递和定义@company 以及@quote 变量可能有点冗长。但我想如果一个人的依赖和路由到另一个人没有办法解决它?
    • @jbk 这就是你必须这样做的方式。不幸的是,实例变量(@company@quote 在这里)在控制器中不可用:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    相关资源
    最近更新 更多