#Show get called controller method

I have a link to my #index

view:

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

      

routes.rb

which relates 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 belongs to

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 on the link, it takes me back to /tickets/calendar.ics

, but that produces an 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

      

Highlighted @ticket = Ticket.find(params[:id])

. It makes sense that he cannot name a ticket with an ID calendar

.

The request has parameters:

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

How do I fix this error? Why does he trigger the action of the show?

+3


source to share


1 answer


The canon has a footnote: Rails Routing from the Out In In :

Rails routes are matched in the order they are listed, so if you have resources: photos above getting "photos / polls", the show route for the resource line will be matched up to the get string. To fix this, move the get line above the resource line so that it matches first.



As noted, the fix is ​​to specify get 'tickets/calendar' => ...

before resources :tickets

. If the order of the routes is in question, you can run rake routes

which as far as I know should show your routes in the order they are checked.

+4


source







All Articles