找不到自定义路线

时间:2017-03-02 05:43:10

标签: ruby-on-rails

Rails 3.2

在我的routes.rb文件中,我有:

namespace :admin do
  resources :tickets, except: [:edit] do
    member do
      put 'save_and_close', to: 'tickets#save_and_close'
    end
  end   
end

在我的controllers / admin / tickets_controller.rb中,我有:

def save_and_close
  success_message = I18n.t('notices.ticket_successfully_updated')
  @ticket = Ticket.find params[:id]
  @ticket.assign_attributes(params[:ticket])
  if @ticket.save
    redirect_to admin_tickets_path, notice: success_message
  else
    flash.now[:error] = @ticket.errors.full_messages.join(', ')
    render action: "show"
  end
end

在我的观看/ admin / tickets / show.html.slim中,我有:

= link_to 'Save & Close', save_and_close_admin_ticket_path, class: 'btn btn-success ticket-event btn-lg'

从rake路线,我有:

save_and_close_admin_ticket PUT  /admin/tickets/:id/save_and_close(.:format)  admin/tickets#save_and_close

但是,当我点击Save&关闭按钮,我收到以下错误消息:

Started GET "/admin/tickets/163925/save_and_close" for 73.83.66.151 at 2017-03-02 05:26:22 +0000
Processing by ApplicationController#routing_error as HTML
Parameters: {"unmatched_route"=>"admin/tickets/163925/save_and_close"}
Completed 404 Not Found in 28.2ms
ActionController::RoutingError (No route matches admin/tickets/163925/save_and_close):

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

试试这个

= link_to 'Save & Close', save_and_close_admin_ticket_path(@ticket), method: :put, class: 'btn btn-success ticket-event btn-lg'

答案 1 :(得分:0)

路由中的方法类型PUT和使用GET的呼叫路由。路径中也没有提供票证ID。

link_to默认情况下使用GET请求进行处理,因此您需要手动指定链接中的方法类型。这样做:

= link_to 'Save & Close', save_and_close_admin_ticket_path(ticket), method: :put, class: 'btn btn-success ticket-event btn-lg'