2015-06-04 61 views
3

我有我的看法#index链接:控制方法#show获取调用

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

routes.rb,涉及到这一点:

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' 

TicketsController是涉及:

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 

当我点击链接时,它会带我到/tickets/calendar.ics这是正确的,但我得到以下错误:

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 

@ticket = Ticket.find(params[:id])高亮显示。这是有道理的,它是不能打电话给一个ID为calendar的票。

请求的参数为:

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

如何解决这个问题?为什么它要求演出?

+0

如果你把'get'tickets/calendar'=> ...''上面的'资源:tickets'?你的'耙路线'也许是有用的信息,否则。 – Kimball

+0

是的。工作!你有没有关于未来可能有帮助的路线顺序的文件? –

+0

我会用一个或两个链接发布更详细的答案 – Kimball

回答

4

有在规范Rails Routing from the Outside In大意一个注脚:

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.

至于评论,修复是提前resources :tickets指定get 'tickets/calendar' => ...。如果路线顺序有问题,您可以运行rake routes,据我所知,这应该按照它们检查的顺序呈现路线。