【问题标题】:Rails 4 link_to No route matchesRails 4 link_to 没有路线匹配
【发布时间】:2015-12-31 05:04:59
【问题描述】:

我收到错误 No route matches {:action=>"index", :controller=>"search"}

使用时

link_to "Next Page", {:controller => 'search', :action => 'index'}

My routes.rb contains 
resources :search, only: [:index]

当我使用

link_to "Next Page", '/search'

Rake 路由结果

                          Prefix Verb  URI Pattern                                         Controller#Action
               errors_cookie GET   /errors/cookie(.:format)                            errors#cookie
                             GET   /categories(/:level1)(/:level2)(/:level3)(.:format) search#categories
                             GET   /category/filter/:type(.:format)                    search#category_filter_sort
                       brand GET   /brand(.:format)                                    search#brand
                             GET   /brand/filter/:type(.:format)                       search#brand_filter_sort
                search_index GET   /search(.:format)                                   search#index
              search_suggest GET   /search/suggest(.:format)                           search#suggest
                 search_list GET   /search/list(.:format)                              search#list
                             GET   /search/filter/:type(.:format)                      search#filter_sort
                             GET   /sd/:slug(.:format)                                 search#show
                             GET   /sd/compare/:id(.:format)                           search#compare
           delete_favourites POST  /favourites/delete(.:format)                        favourites#delete
           usuals_favourites GET   /favourites/usuals(.:format)                        favourites#usuals
           orders_favourites GET   /favourites/orders(.:format)                        favourites#orders
                  favourites GET   /favourites(.:format)                               favourites#index
                             POST  /favourites(.:format)                               favourites#create
Routes for Spree::Core::Engine:
                spree_user_omniauth_authorize GET|POST /users/auth/:provider(.:format)                                             spree/omniauth_callbacks#passthru {:provider=>/facebook|twitter|github|google_oauth2|amazon/}
                 spree_user_omniauth_callback GET|POST /users/auth/:action/callback(.:format)                                      spree/omniauth_callbacks#:action    
Routes for Ahoy::Engine:
visits POST /visits(.:format) ahoy/visits#create
events POST /events(.:format) ahoy/events#create

【问题讨论】:

  • 如果您运行rake routes,它将显示您的应用程序中定义的所有路由,您可以尝试运行它,并将输出添加到您上面的问题中,因为这可能有助于我们调试您的问题?
  • 在 kaminari link_to_next_page 失败并出现相同错误后,我尝试使用 link_to 进行调试。 Kaminari paginate @object 工作正常。
  • 添加了缩短的 rake 路线。由于大小限制,无法发布整个内容。

标签: link-to-function


【解决方案1】:

它是一个 spree 应用程序,控制器继承自 Spree::BaseController。 url_for 只看到 spree 引擎中的路由,而不是全局路由

通过将 main_app 添加到 url_for main_app.url_for(...) 解决了这个问题

将此monkey patch 用于kaminari

感谢您抽空亚历山大。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2012-10-11
    • 2017-07-13
    • 2015-11-08
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多