2016-07-22 58 views
1

我在routes.rb中安裝FullcalendarEngine:如何獲取url_for使用模塊?

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"} 

通知的命名空間的控制器值。這不起作用:

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來識別路線?

回答

0

你的路線期待一個id參數,所以你需要給它一個:

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

此外,您發佈的路線不包括用於index的路線,所以它可能不存在。如果是這樣,它可能不需要id

+0

其實我最初並沒有發佈所有的路線。確實有一個索引路線。我更新了這個問題。 – Donato

相關問題