更改更新路径 rails
change routes of update rails
我对更改路由值有疑问。
我有这种情况:
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| %>
我必须更改路线以获得正确的 supply_path form_for?
谢谢。
编辑:
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
您可以简单地使用 <%= form_for @supply do |f| %>
作为 edit.html.erb 文件。原因是:当你在SuppliesController
的edit
方法中实例化@supply
时,Rails会自动将post的形式转为update
的方法,你没有需要明确告诉它。同样,在 new.html.erb 中,您也将使用相同的方法:<%= form_for @supply do |f| %>
,但现在在您的 new
方法中,您将执行 @supply = Supply.new
,Rails 将 post这个窗体的创建方法。
您确实需要定义路线,但就正确的路径而言,只要您在 form_for
中提供正确的 @supply
变量,Rails 就会处理它。
编辑:
在你的路由文件中:
resources :supplies
我对更改路由值有疑问。 我有这种情况:
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| %>
我必须更改路线以获得正确的 supply_path form_for?
谢谢。
编辑:
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
您可以简单地使用 <%= form_for @supply do |f| %>
作为 edit.html.erb 文件。原因是:当你在SuppliesController
的edit
方法中实例化@supply
时,Rails会自动将post的形式转为update
的方法,你没有需要明确告诉它。同样,在 new.html.erb 中,您也将使用相同的方法:<%= form_for @supply do |f| %>
,但现在在您的 new
方法中,您将执行 @supply = Supply.new
,Rails 将 post这个窗体的创建方法。
您确实需要定义路线,但就正确的路径而言,只要您在 form_for
中提供正确的 @supply
变量,Rails 就会处理它。
编辑:
在你的路由文件中:
resources :supplies