【问题标题】:Routing error rails 3路由错误轨道 3
【发布时间】:2015-07-15 21:34:40
【问题描述】:

我有一个带有许多控制器、模型等的 Rails 2.3.5 应用程序,我正在尝试将其升级到 Rails 3.2.21。我的路线有些问题。我试图遵循 Rails 3 新的路由格式,但它似乎不起作用。我遇到了两个问题(我想这都表明我的路由存在一个基本问题):

  1. 在根 ('/') 中,我得到了通用的“欢迎出国”Rails 页。我的路由(见下文)为 root 定义了路由。
  2. 对于某些控制器,我会收到No route matches [GET] "/study" 消息。我的路线显示了这条路线,但由于某种原因没有定义GET 方法。

这是我的config/routes.rb 代码:

Myapp::Application.routes.draw do    
  root :to => 'study#index'

  match 'login' => 'login', :protocol => 'https://'

  resources :study_maps do
    get :clone, :on => :member
  end

  # Route report create actions to the report controller
  match 'report/create', :as => 'report'

  match ':controller(/:action(/:id))(.:format)'
end

如果我正在运行rake routes,我会得到:

           root        /                                      study#index
          login        /login(.:format)                       login#login {:protocol=>"https://"}
clone_study_map GET    /study_maps/:id/clone(.:format)        study_maps#clone
     study_maps GET    /study_maps(.:format)                  study_maps#index
                POST   /study_maps(.:format)                  study_maps#create
  new_study_map GET    /study_maps/new(.:format)              study_maps#new
 edit_study_map GET    /study_maps/:id/edit(.:format)         study_maps#edit
      study_map GET    /study_maps/:id(.:format)              study_maps#show
                PUT    /study_maps/:id(.:format)              study_maps#update
                DELETE /study_maps/:id(.:format)              study_maps#destroy
         report        /report/create(.:format)               report#create
                       /:controller(/:action(/:id))(.:format) :controller#:action

这是我的 StudyController#index 代码:

require 'myapp/studymgr'
require 'project_user'
require_dependency 'myapp/controller_extensions/report_manager'

class StudyController < ApplicationController
  include PaginatorController

  before_filter(:authenticate, :except => [:todo])
  before_filter(:authorize,
                :only => [:update, :destroy, :edit, :prune, :select_experiments ])

  def index
    @tags = Stag.find(:all).collect { |stag| stag.tag }
    ...
    @include_ext = true
  end
end

有人可以就我缺少的内容提出建议吗?

【问题讨论】:

  • 我没有看到你在哪里为StudyController 定义了路由。对于StudyMapsController,您需要发布该代码以帮助我们找出未初始化的原因。
  • 对于路线,您会看到 StudyMapsController 已定义,还有一条通用路线也应涵盖 StudyController。你的意思是实际的控制器代码吗?
  • 是的,我建议您发布StudyMapsController 的代码。我现在看到了一般路线,但从未见过 match 这样使用。这似乎是Study 操作问题的根源(没有双关语)。
  • 我解决了 StudyMapsController 问题 - 旧应用程序的名称是 StudyMapController。我的错...我添加了StudyController 代码。试图将其(和路线)更改为StudiesController,但没有帮助。另外,您能否详细说明一下默认的match 用法?你建议怎么写?我还尝试了 Rails 3 安装提供的默认格式(使用 rails_upgrade 插件):match ':controller/:action/:id',但得到了完全相同的结果。
  • 我会明确定义您需要的学习路线或使用resources :studies 来获取学习模型的所有Restful 路线。当您进行手动更改以修复复数问题时,请使用 grep 以确保您捕获所有更改并仔细检查所有文件夹名称。当然,请记住在更改后重新启动服务器。

标签: ruby-on-rails-3 routes


【解决方案1】:

终于找到了解决方案——我的观点有.rhtml 扩展名。我发现 Rails 3 (What is the difference Between .erb , .rhtml and .html.erb?) 不再支持这种格式,所以我将所有视图扩展更改为 .html.erb,然后路由无需为每个控制器指定显式资源,只需使用通用路由即可: match ':controller(/:action(/:id))'.

至于根路由(上面的#1)中的问题,尽管我的routes.rb 中有明确的路由,但我总是得到Rails“欢迎国外”页面,结果我不得不删除public/index.html 这是由 Rails 为 root 加载,然后我的视图被加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多