我在我的应用程序中添加了一条到默认的restful路由的路由,所以在我的控制器中我有一个名为status的动作,它的动词是Patch,但每次我尝试更新属性时都会出现上述错误通过状态操作,注意:Restful路由中的默认更新操作没有问题。
的routes.rb
Rails.application.routes.draw do
devise_for :users
resources :projects do
resources :gigs, shallow: true do
patch :status
end
end
end
Gigs Controller
class GigsController < ApplicationController
before_action :set_gig, only: [:show, :edit, :update, :destroy,:status]
def status
respond_to do |format|
if @gig.update(:done, true)
format.html { redirect_to @gig, notice: 'Gig was successfully updated.' }
format.json { render :show, status: :ok, location: @gig }
else
format.html { render :edit }
format.json { render json: @gig.errors, status: :unprocessable_entity }
end
end
end
private
def set_gig
@gig = Gig.find(params[:id])
end
def gig_params
params.require(:gig).permit(:name, :description, :done,:timeline)
end
end
这是我添加到index.html.erb
以执行更新
<td><%= link_to 'Mark as Done', gig_status_path(gig), method: :patch %></td>
这是日志
Started PATCH "/gigs/2/status" for 127.0.0.1 at 2018-04-07 12:26:20 +0100
Processing by GigsController#status as HTML
Parameters: {"authenticity_token"=>"iNYFcsiwtNnCAC9goTGtfrqHFnBKufpgQ+61/pwZLVV6Nw82MBTART5ozVQUVsk74UFiWDknWrtoqYLN9D/2YQ==", "gig_id"=>"2"}
[1m[36mGig Load (0.0ms)[0m [1m[34mSELECT "gigs".* FROM "gigs" WHERE "gigs"."id" = $1 LIMIT $2[0m [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound - Couldn't find Gig with 'id'=:
app/controllers/gigs_controller.rb:93:in `set_gig'
Started POST "/__better_errors/df8ad027b952f89c/variables" for 127.0.0.1 at 2018-04-07 12:26:20 +0100
答案 0 :(得分:1)
您可以使用命令rake routes
检查URI模式以进行状态操作。
Prefix Verb URI Pattern Controller#Action
gig_status PATCH /gigs/:gig_id/status(.:format) gigs#status
project_gigs GET /projects/:project_id/gigs(.:format) gigs#index
POST /projects/:project_id/gigs(.:format) gigs#create
new_project_gig GET /projects/:project_id/gigs/new(.:format) gigs#new
edit_gig GET /gigs/:id/edit(.:format) gigs#edit
gig GET /gigs/:id(.:format) gigs#show
PATCH /gigs/:id(.:format) gigs#update
PUT /gigs/:id(.:format) gigs#update
因此,您需要在状态操作
的情况下使用gig_id
修改方法set_gig
,如下所示:
def set_gig
@gig = Gig.find(params[:gig_id])
end