gpt4 book ai didi

ruby-on-rails - Controller 方法#show 被调用

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:32 25 4
gpt4 key购买 nike

我的 #index View 上有一个链接:

<%= 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,这是正确的,但我收到以下错误:

TicketsController#show 中的 ActiveRecord::RecordNotFound

找不到“id”=calendar 的 Ticket

提取的源代码(大约第 83 行):

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

@ticket = Ticket.find(params[:id]) 突出显示。这是有道理的,它无法调用 ID 为 calendar 的工单。

请求有参数:

{"id"=>"日历",
“格式”=>“ics”

如何修复此错误?为什么叫表演 Action ?

最佳答案

规范中有一个脚注 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,据我所知,它应该按照检查的顺序呈现您的路由。

关于ruby-on-rails - Controller 方法#show 被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30633639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com