ruby-on-rails - 创建自定义链接到按钮,并得到错误的路由错误红宝石
问题描述
我有 2 个 link_to 按钮,一个是更新项目记录,另一个是通过将活动字段更新为 false 来存档项目记录,更新按钮工作完美,但我收到存档链接到按钮错误。我收到此错误
No route matches [POST] "/items/10/listing"
这是 items_controller.rb 方法
def archive
@item = Item.find(params[:id])
@item.active = false
if @item.update
flash[:notice] = "Item been archived..."
else
flash[:alert] = "Something went wrong..."
end
redirect_back(fallback_location: request.referer)
end
这里是 routes.rb
Rails.application.routes.draw do
resources :items, except: [:edit] do
member do
get 'listing'
get 'pricing'
get 'description'
get 'photo_upload'
get 'location'
get 'preload'
get 'preview'
end
resources :photos, only: [:create, :destroy]
resources :reservations, only: [:create]
resources :calendars
end
end
这是 index.html.rb
<div class="panel-body">
<% @items.each do |item| %>
<div class="row" style="border: 1px solid #dddbda;padding:0.8rem;display: contents;">
<div class="col-md-2">
<%= image_tag item.cover_photo(:thumb) %>
</div>
<div class="col-md-7">
<h4><%= item.item_name %></h4>
</div>
<div class="col-md-3 right">
<div>
<%= link_to "Update", listing_item_path(item), class: "btn btn-form" %>
</div>
<div style="padding-top: 1rem;">
<%= link_to "Archive", listing_item_path(item), method: :archive, class: 'btn-delete' , :data => {:confirm => 'Are you sure?'}%>
</div>
</div>
</div>
<hr/>
<% end %>
</div>
解决方案
首先,我会将您更改routes.rb
为更像:
resources :items, except: [:edit] do
member do
get :listing
get :pricing
get :description
get :photo_upload
get :location
get :preload
get :preview
post :archive
end
resources :photos, only: [:create, :destroy]
resources :reservations, only: [:create]
resources :calendars
end
这将给你(除其他外):
listing_item GET /items/:id/listing(.:format) items#listing
pricing_item GET /items/:id/pricing(.:format) items#pricing
description_item GET /items/:id/description(.:format) items#description
photo_upload_item GET /items/:id/photo_upload(.:format) items#photo_upload
location_item GET /items/:id/location(.:format) items#location
preload_item GET /items/:id/preload(.:format) items#preload
preview_item GET /items/:id/preview(.:format) items#preview
archive_item POST /items/:id/archive(.:format) items#archive
item_photos POST /items/:item_id/photos(.:format) photos#create
item_photo DELETE /items/:item_id/photos/:id(.:format) photos#destroy
item_reservations POST /items/:item_id/reservations(.:format) reservations#create
item_calendars GET /items/:item_id/calendars(.:format) calendars#index
POST /items/:item_id/calendars(.:format) calendars#create
new_item_calendar GET /items/:item_id/calendars/new(.:format) calendars#new
edit_item_calendar GET /items/:item_id/calendars/:id/edit(.:format) calendars#edit
item_calendar GET /items/:item_id/calendars/:id(.:format) calendars#show
PATCH /items/:item_id/calendars/:id(.:format) calendars#update
PUT /items/:item_id/calendars/:id(.:format) calendars#update
DELETE /items/:item_id/calendars/:id(.:format) calendars#destroy
items GET /items(.:format) items#index
POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
item GET /items/:id(.:format) items#show
PATCH /items/:id(.:format) items#update
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
然后改变:
<%= link_to "Archive", listing_item_path(item), method: :archive, class: 'btn-delete' , :data => {:confirm => 'Are you sure?'}%>
至:
<%= link_to "Archive", archive_item_path(item), method: :post, class: 'btn-delete' , :data => {:confirm => 'Are you sure?'}%>
您会在文档中(在“选项”下)注意到method
是 HTTP 动词的符号,可以是:post
、:delete
、:patch
或:put
。但不是:archive
。这不是 HTTP 动词。
推荐阅读
- r - rename_with 但谓词基于其他变量中的值
- xml - 在 RML(RDF 映射语言)中加入多个 (2+) 节点/多跳映射
- macos - Flutter 无法运行,get_navigation & 主题错误
- windows - 外壳 | 在目录中按名称(Name.txt/Name(01).txt/Name(02).txt)查找类似文件,并删除除上次修改外的所有文件
- javascript - 为什么我在 componentDidMount 中的操作调用后 redux 状态未定义?
- python - 如何访问自定义对象列表中的字典值?
- jmeter - JMeter 报告显示一个请求的 3 个结果
- php - 无法在共享主机上通过 php 运行 bash 脚本
- powershell - 找不到“XmlMessageFormatter”的重载和参数计数:Powershell 中的 N 和 New-Object
- c - 在具有最新 XCode 的 MacOS Catalina 上,__DARWIN_C_LEVEL 对于 String.h 来说太低了……我该怎么办?