首页 > 解决方案 > 创建自定义链接到按钮,并得到错误的路由错误红宝石

问题描述

我有 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>

标签: ruby-on-railsrubyroutes

解决方案


首先,我会将您更改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 动词。


推荐阅读