【问题标题】:uninitialized constant ProfileController - one to one association未初始化的常量 ProfileController - 一对一关联
【发布时间】:2018-01-23 21:27:26
【问题描述】:

我知道有一些类似的问题,但我找不到我的案例。我也检查了那个网站:https://github.com/bitmakerlabs/debugging-guide/wiki/Rails-error:-Routing-Error:-uninitialized-constant 看起来它应该可以工作。找不到问题出在哪里。

错误看起来像:

未初始化的常量 ProfileController

在我的 app/controllers 文件夹中,我有 profiles_controller.rb 文件:

class ProfilesController < ApplicationController
  # GET to /users/:user_id/profile/new
  def new
    # Render blank profile details form
  end
end

我在 app/views/profiles/new.html.erb 下有一个视图文件,并使用 &lt;%= link_to "Create your profile!", new_user_profile_path(user_id: current_user.id) if user_signed_in? %&gt; 链接到它。在我的路线中,我有:

new_user_profile_path GET /users/:user_id/profile/new(.:format)
个人资料#新

你能告诉我一个错误吗?我能找到的每个答案都与命名有关,而我的名字对我来说很好(profiles_controller.rb 和 ProfilesController 类)。我错过了一些明显的东西吗?

编辑:

我的 routes.rb 文件 sn-p 用于配置文件:

resources :users do
  resources :profile
end

编辑 2:

在我的项目中,一位用户可能只有一份个人资料。路由 ...user/profile/... 比 ...user/profiles/... 更有意义

【问题讨论】:

  • 你可以为resource :users添加你的routes.rb文件的sn-p吗?
  • @AnuragAryan 我在问题的末尾添加了这一点。
  • @RockwellRice 它确实被称为 ProfilesController,我相信它应该是复数?但我注意到错误是关于 ProfileController 但我不知道为什么......
  • @RockwellRice 不,从一开始它就是“profiles”文件夹。但是我在 routes.rb 中有“profile”而不是“profiles”。已解决,谢谢!如果您可以将该评论添加为答案,我会将其作为正确答案进行检查。

标签: ruby-on-rails routes


【解决方案1】:

您需要更改路线的名称。

routes.rb

profiles#new

【讨论】:

    【解决方案2】:

    还有另一种方法可以解决这个问题。因为每个用户在 route.rb 中都有一个配置文件(该关联在我的项目中,但我没有提到它,因为我认为它不相关)而不是将“配置文件”更改为“配置文件”,只需更改“资源:配置文件”转换为“资源:配置文件”。这样以后在我们的路线中,我就有了 user/profile/... 这更有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多