没有路线匹配[POST]" parent / id / child / new"嵌套关联错误

时间:2017-07-04 08:00:44

标签: ruby-on-rails routing

公司has_many行情,尝试在报价控制器中创建新报价,嵌套路线到位,但我得到了;

No route matches [POST] "/companies/123/quotes/new"

的routes.rb

resources :companies do
    resources :quotes, only: [ :new, :create, :show, :index]
    resources :employees, only: [:show, :index]
end

rake routes

                  Prefix Verb   URI Pattern                                    Controller#Action

          company_quotes GET    /companies/:company_id/quotes(.:format)        quotes#index
                         POST   /companies/:company_id/quotes(.:format)        quotes#create
       new_company_quote GET    /companies/:company_id/quotes/new(.:format)    quotes#new
           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

new.html.erb

<div class='form-group col-md-6 quote-form'>
    <%= simple_form_for [@company, @quote] do |quote| %>
        <%= render 'quote_fields', :f => quote %>
        <%= quote.submit "Get Quote", class: 'btn btn-primary' %>
    <% end %>
</div>

_quote_fields.html.erb

<%= f.input :lives_overseas, as: :radio_buttons, collection: [['Yes', true], 
            ['No', false]], readonly: nil %>
<%= f.input :payment_frequency, collection: Quote.payment_frequencies.map { |k,v| [ k.humanize, k ] } %>
<%= params.inspect %>

quotes_controller.rb

class QuotesController < ApplicationController
before_action :authenticate_user!, only: [ :new, :create, :show, :index ]

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

    def create
    @company = Company.find(params[:company_id])
    @quote = @company.quotes.build(params[:quote])
    if @quote.save
      render 'show'
    end
    end

  def show
    @company = Company.find(params[:company_id])
    @quote = @company.quotes.find(params[:id])
    @employees = @company.employees.all
    # puts debug(params)
  end

  def index
    @company = Company.find(params[:company_id])
    @quotes = @company.quotes.all
    @employees = @company.employees.all
  end
end

不应将报价单上的提交操作发布到; POST /companies/:company_id/quotes(.:format),这显然来自耙路线?

我在这里做错了什么想法?感谢您的指导。

1 个答案:

答案 0 :(得分:2)

  

没有路线匹配[POST]&#34; / companies / 123 / quotes / new&#34;

您拥有嵌套资源,因此您需要更改

<%= simple_form_for @quote do |quote| %>

<%= simple_form_for [@comapny,@quote] do |quote| %>

并在@quote = @company.quotes.build方法

中将@quote = Quote.new更改为quotes#new