【问题标题】:Rails routing error with long class names带有长类名的 Rails 路由错误
【发布时间】:2011-08-04 17:45:59
【问题描述】:

我对 Rails 的多元化和驼峰化感到困惑。尤其是我长而清晰的名字。

我有一个User 模型和一个Account 模型。我还有一个user_to_account_log_history 模型和控制器,用于保存两者之间的传输。关系已建立。

我跑了……

$ rails generate controller UserToAccountLogHistories

...它创建了以下内容:

# app/controllers/user_to_account_log_histories_controller.rb
class UserToAccountLogHistoriesController < ApplicationController
  # a simple index method
end

# app/models/user_to_account_log_history.rb
class UserToAccountLogHistory < ActiveRecord::Base
end

我的路线似乎已经到位(rake routes 输出,被截断):

user_usertoaccountloghistories GET /users/:user_id/usertoaccountloghistories(.:format) {:action=>"index", :controller=>"usertoaccountloghistories"}

但我收到了uninitialized constant UsertoaccountloghistoriesController。为什么?我觉得是长名字的驼峰式和复数化把事情搞砸了。

【问题讨论】:

    标签: ruby-on-rails rails-routing camelcasing


    【解决方案1】:

    问题是您有一个名为UserToAccountLogHistoriesController 的类,但没有一个名为UsertoaccountloghistoriesController 的类——请注意这里的大小写差异。

    从你的问题中不清楚你是如何定义你的路线的,但我怀疑你有一个参考usertoaccountloghistories 的路线,而实际上你想参考user_to_account_log_histories

    在 Rails 控制台中,观察以下内容:

    > "hello_world".camelize
    # => "HelloWorld"
    > "helloworld".camelize
    # => "Helloworld"
    

    【讨论】:

    • 谢谢!我输入的路线没有下划线。
    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2018-03-15
    • 2011-10-10
    • 1970-01-01
    相关资源
    最近更新 更多