【问题标题】:Simple Form and routing errors简单的表单和路由错误
【发布时间】:2016-03-24 18:15:28
【问题描述】:

视图/表单

<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

Fuel Price Model has 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) 供应商/fuel_prices#create

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

edit_supplier_fuel_price_path GET /supplier/fuel_prices/:id/edit(.:format) supplier/fuel_prices#edit 供应商燃料价格路径

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 simple-form simple-form-for


    【解决方案1】:

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

    【讨论】:

    • 我做了这个 然后我收到新错误说 ActionView::Template::Error: undefined method `user_fuel_prices_path' for #:0x0055eeaf4b42b0>
    • 我有 supplier_fuel_prices_path ,但我如何使它与供应商而不是用户一起工作。我有用户模型和类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2017-08-05
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多