2015-06-04 81 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,據我所知,這應該按照它們檢查的順序呈現路線。