更改更新轨道的路由

时间:2015-06-24 09:19:44

标签: ruby-on-rails ruby-on-rails-4 routes updates

我对更改路线值有疑问。 我有这种情况:

routes.rb

  patch 'supplies/update'
  get 'supplies/edit'
  get 'supplies/index'


views/supplies/edit.html.erb

    <%= form_for @supply , :url => supplies_update_path(id: @supply) do |f| %>

    ....

我将在edit.html.erb文件中使用以下代码:

<%= form_for @supply do |f| %>

我必须在路线中更改以获取此form_for的正确supply_path?

谢谢你。

编辑:

enter image description here

class SuppliesController < ApplicationController
    before_action :set_supply, only: [:show, :edit, :update, :destroy]

    load_and_authorize_resource


    # GET /supplies/index
  def index
            @supplies = Supply.includes(:product).all
  end

    def edit
  end

    # PATCH/PUT /supplies/1
  def update
    respond_to do |format|
      if @supply.update(supply_params)
        format.html { redirect_to supplies_index_path, notice: 'Quantità Aggiornata.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @supply.errors, status: :unprocessable_entity }
      end
    end
  end

   private


  def set_supply
      @supply = Supply.includes(:product).find(params[:id])
        rescue ActiveRecord::RecordNotFound
            render :template => 'public/404.html'
    end


  def supply_params
    params.require(:supply).permit(:quantity)
  end



end

1 个答案:

答案 0 :(得分:1)

您只需将<%= form_for @supply do |f| %>用于edit.html.erb文件即可。原因是:当您在@supply中的edit方法中实例化SuppliesController时,Rails会自动将表单发布到update方法,您无需明确告知它。同样,在new.html.erb中,您也会使用相同的:<%= form_for @supply do |f| %>,但现在在new方法中,您将执行@supply = Supply.new,Rails会发布此表单以创建方法。

您确实需要定义路由,但就正确的路径而言,只要您在@supply中提供正确的form_for变量,Rails就会负责处理。

修改

在路线档案中:

resources :supplies