控制器方法#show被调用 [英] Controller method #show getting called

查看:54
本文介绍了控制器方法#show被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 #index 视图上有一个链接:

  <%=链接到导出日历(ICS),{控制器::票证,操作::ics_export,格式::ics},类:需要类正确的权限%> 

routes.rb 与此相关:

 资源:机票
获得机票/日历 => ‘tickets#ics_export’
帖子‘tickets’=> 'tickets#index'
补丁'tickets /:id / close'=> tickets#close,例如: close_ticket
个帖子 tickets /:id => 'ticket_comments#create'

我的 TicketsController 有关:

  before_action:set_ticket,仅:[:show,:edit,:destroy,:update,:close] 

def show
@ticket_comment = TicketComment.new
end

def ics_export
tickets = Ticket.all
response_to | format |
format.html
format.ics做
cal = Icalendar :: Calendar.new
tickets.each | ticket |
event = Icalendar :: Event.new
event.dtstart = ticket.start
event.description = ticket.summary
cal.add_event(event)
end
cal.publish
render:text => cal.to_ical
结束
结束
结束

私人
def set_ticket
@ticket = Ticket.find(params [:id])
end

当我单击链接时,它将带我到 /tickets/calendar.ics 是正确的,但是出现以下错误:



ActiveRecord :: RecordNotFound in TicketsController#show



找不到带有'id'= calendar <的票证/ p>

提取的来源(第83行附近):

 私人
def set_ticket
@ticket = Ticket.find(params [:id])
结束

突出显示 @ticket = Ticket.find(params [:id])。这说明无法调用ID为 calendar 的票证。



请求具有参数:



{ id => calendar,
format => ics}



如何解决此错误?为什么称其为表演动作?

解决方案

规范的从外部进入的路线到效果:


路线的顺序匹配它们是指定的,因此,如果您具有资源:在获取照片/投票上方的照片,则该资源行的show动作路线将在获取行之前匹配。要解决此问题,请将get行移到资源行上方,使其首先匹配。


如所评论的,解决方法是指定获取门票/日历 => ... ,先于 resources:tickets 。如果路线的顺序有疑问,您可以运行 Rake路线,据我所知,应该按照检查的顺序显示路线。


I have a link on my #index view:

<%= link_to 'Export Calendar (ICS)', { controller: :tickets, action: :ics_export, format: :ics }, class: "class-needed right" %>

routes.rb that pertains to this:

resources :tickets
get 'tickets/calendar' => 'tickets#ics_export'
post 'tickets' => 'tickets#index'
patch 'tickets/:id/close' => 'tickets#close', as: 'close_ticket'
post 'tickets/:id' => 'ticket_comments#create'

My TicketsController that pertains:

before_action :set_ticket, only: [:show, :edit, :destroy, :update, :close]

def show
  @ticket_comment = TicketComment.new
end

def ics_export
  tickets = Ticket.all
  respond_to do |format|
    format.html
    format.ics do
      cal = Icalendar::Calendar.new
      tickets.each do |ticket|
        event = Icalendar::Event.new
        event.dtstart = ticket.start
        event.description = ticket.summary
        cal.add_event(event)
      end
      cal.publish
      render :text =>  cal.to_ical
    end
  end
end

private
def set_ticket
  @ticket = Ticket.find(params[:id])
end

And when I click the link, it takes me to /tickets/calendar.ics which is correct but I get the following error:

ActiveRecord::RecordNotFound in TicketsController#show

Couldn't find Ticket with 'id'=calendar

Extracted source (around line #83):

 private
 def set_ticket
   @ticket = Ticket.find(params[:id])
 end

The @ticket = Ticket.find(params[:id]) is highlighted. Which make sense that it is failing to call a ticket with an id of calendar.

Request has parameters:

{"id"=>"calendar", "format"=>"ics"}

How do I fix this error? Why is it calling the show action?

解决方案

There is a footnote in the canonical Rails Routing from the Outside In to the effect:

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

As commented, the fix is to specify get 'tickets/calendar' => ... ahead of resources :tickets. If the order of routes is in question, you can run rake routes, which, to the best of my knowledge, should render your routes in the order they are checked.

这篇关于控制器方法#show被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆