-1

查看/表格

<div class="col-lg-12">
            <div class="ibox-content">
                <div class="row">
                        <%= simple_form_for supplier_fuel_prices_path(@supplier,@fuel_price), method: :post do |f| %>
                          <%= f.input :regular, label: "Regular" %>
                          <%= f.input :medium, label: "Medium"%>
                          <%= f.input :premium, label: "Premium" %>
                          <%= f.input :diesel, label: "Diesel" %>
                        <%= f.button :submit, "Update"%>
                        <%end%>
                    </div>
                </div>
            </div>
        </div>
    </div>

控制器

class Supplier::FuelPricesController < Supplier::ApplicationController
  before_action :set_supplier

  def index
  end

  def new
    @fuel_price = @supplier.fuel_prices.build
  end

  def create
    @fuel_price = @supplier.fuel_price.build(fuel_price_params)
    if @fuel_price.save
      flash[:notice] = "You have successfully Added new Fuel Price."
      redirect_to supplier_fuel_prices_path
    else
      flash.now[:alert] = "Something went wrong. Please try again."
      render "new"
    end
  end

  private

  def fuel_price_params
    params.require(:fuel_price).permit(:regular, :medium, :premium, :diesel)
  end

  def set_supplier
    @supplier = User.find_by(params[:supplier_id])
  end
end

楷模

用户模型有 has_many :fuel_prices, foreign_key: :supplier_id

燃油价格模型有belongs_to "supplier", class_name: "User"

提交表单时出现的错误是

没有路线匹配 [POST] "/supplier/fuel_prices/new"

我的路线看起来像这样

 namespace :supplier do
    root to: 'dashboard#index', as: 'dashboard'
    resources :retailers
    resources :fuel_prices

  end

路线

Supplier_fuel_prices_path GET /supplier/fuel_prices(.:format) POST /supplier/fuel_prices(.:format) supplier/fuel_prices#create

new_supplier_fuel_price_path GET /supplier/fuel_prices/new(.:format) 供应商/fuel_prices#new

edit_supplier_fuel_price_path GET /supplier/fuel_prices/:id/edit(.:format) supplier/fuel_prices#edit supplier_fuel_price_path

4

1 回答 1

0

你只是想post走错路。以 new 结尾的 url 通常是一个get请求。运行rake routes并确保您发布到正确的post路线并更新您的表单。

于 2016-03-24T18:18:47.297 回答