gpt4 book ai didi

ruby-on-rails - 如何让 url_for 与模块一起工作?

转载 作者:行者123 更新时间:2023-12-05 07:48:29 24 4
gpt4 key购买 nike

我在我的 routes.rb 中安装了 FullcalendarEngine:

mount FullcalendarEngine::Engine , at: "/fullcalendar_engine"

不幸的是,即使我在 routes.rb 中有这个:

resources :events,  module: 'fullcalendar_engine'

以及生成的路由:

fullcalendar_engine_path                    /fullcalendar_engine        FullcalendarEngine::Engine
events_path GET /events/index(.:format) fullcalendar_engine/events#index
event_path GET /events/:id(.:format) fullcalendar_engine/events#show
events_path GET /events(.:format) fullcalendar_engine/events#index
POST /events(.:format) fullcalendar_engine/events#create
new_event_path GET /events/new(.:format) fullcalendar_engine/events#new
edit_event_path GET /events/:id/edit(.:format) fullcalendar_engine/events#edit
event_path GET /events/:id(.:format) fullcalendar_engine/events#show
PATCH /events/:id(.:format) fullcalendar_engine/events#update
PUT /events/:id(.:format) fullcalendar_engine/events#update
DELETE /events/:id(.:format) fullcalendar_engine/events#destroy

我仍然不能对它使用 url_for(我需要这样做才能让它与 will_paginate 一起使用):

错误:

RailsDevise::Application.routes.url_for({ controller: 'events', action: 'index'})
=> ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"events"}
FullcalendarEngine::Engine.routes.url_for({ controller: 'events', action: 'index'})
=> ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"events"}

当我检查模块的路由时:

FullcalendarEngine::Engine.routes.routes.collect {|journey| journey.defaults }
=> [{:controller=>"fullcalendar_engine/events", :action=>"index"}, {:action=>"get_events", :controller=>"fullcalendar_engine/events"}, {:action=>"move", :controller=>"fullcalendar_engine/events"}, {:action=>"resize", :controller=>"fullcalendar_engine/events"}, {:action=>"index", :controller=>"fullcalendar_engine/events"}, {:action=>"create", :controller=>"fullcalendar_engine/events"}, {:action=>"new", :controller=>"fullcalendar_engine/events"}, {:action=>"edit", :controller=>"fullcalendar_engine/events"}, {:action=>"show", :controller=>"fullcalendar_engine/events"}, {:action=>"update", :controller=>"fullcalendar_engine/events"}, {:action=>"update", :controller=>"fullcalendar_engine/events"}, {:action=>"destroy", :controller=>"fullcalendar_engine/events"}]

注意它有这个:

@defaults={:controller=>"fullcalendar_engine/events", :action=>"index"}

注意 Controller 值的命名空间。这不起作用:

FullcalendarEngine::Engine.routes.url_for({:controller=>"events", :action=>"index"})
=> ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"events"}

但是如果我尝试给它命名空间,它会给出这个错误:

FullcalendarEngine::Engine.routes.url_for({:controller=>"fullcalendar_engine/events", :action=>"index"})
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

即使添加主机也会生成错误的 url:

FullcalendarEngine::Engine.routes.url_for({:controller=>"fullcalendar_engine/events", :action=>"index", host: "localhost"})
=> "http://localhost/fullcalendar_engine/"

如何让 url_for 识别路线?

最佳答案

你的路由需要一个 id 参数,所以你需要给它一个:

url_for(controller: 'events', action: 'show', id: 5)   # < Notice the `id`

此外,您发布的路由不包含index 的路由,因此它可能不存在。如果是这样,它可能不需要 id

关于ruby-on-rails - 如何让 url_for 与模块一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38535727/

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