Ruby on Rails 从 SQlite 数据库切换到 Postgresql 会给出未定义的方法“to_sym”;对于 nil:NilClass 错误

发布于 2024-12-13 14:51:04 字数 1469 浏览 0 评论 0原文

我正在开发一个使用 Devise 身份验证系统的 Ruby on Rails 应用程序。 https://github.com/plataformatec/devise

我在互联网上搜索了与我自己类似的问题,但是没有没有用。我想给你一些背景知识,这一切都是从两个独立的应用程序开始的。其中之一是使用设备。我目前正在尝试将这两个应用程序混合为一个。即使将模型视图和控制器放在需要的位置之后,我也没有遇到任何问题。然而,我们需要从 Sqlite3 数据库切换到 Postgresql 数据库。一旦我修改了database.yml文件以指向新的Postgres数据库并重新启动服务器,我就开始在整个应用程序中收到各种to_sym错误,以及使用Devise添加到应用程序的新功能的路由错误。

在切换到 Postgresql 数据库之前,我通过数据库转换器运行了development.sqlite3 文件——这是我过去做过的事情,没有出现任何问题。

我不确定如何调试此应用程序,因为这些 to_sym 错误始终发生。

举个例子,这是停止工作的页面之一:

Home#index 中出现 NoMethodError

显示 C:/Users/chunter33/Desktop/devise_forem/app/views/layouts/application.html.haml 第 19 行提出的位置:

nil:NilClass 的未定义方法 `to_sym'

  • 16: %a{:href => "#"}= t("web-app-theme.profile", :default => "个人资料")
  • 17:%li
  • 18: %a{:href =>; "#"}= t("web-app-theme.settings", :default => "设置")
  • 19: - 如果登录?
  • 20:%li
  • 21:%a
  • 22: = current_user.email
  • 应用程序跟踪:

    app/helpers/application_helper.rb:3:在“signed_in”中?

    app/views/layouts/application.html.haml:19:in `_app_views_layouts_application_html_haml__944875441_29833656'

    注意:如果我切换回 SQLite,我将不再收到任何这些错误。

    我正在使用 Ruby 1.8.7 和 Rails 3.1

    有什么想法可以开始寻找吗?

    如果您需要任何其他信息,请告诉我。

    I'm working on a Ruby on Rails application that used the Devise authentication system.
    https://github.com/plataformatec/devise

    I scoured the internet for a similar problem to my own but no no avail. I guess to give you a little background, it all started out with 2 separate applications. One of which was using devise. I'm currently attempting to mix both applications into one. Even after putting the model views and controllers where they needed to I wasn't running into any problems. However, we needed to switch from a Sqlite3 database to a Postgresql one. As soon as I modified the database.yml file to point to the new Postgres database and restarted the server I started to get various to_sym errors all throughout the application and routing errors for the new functionality that was added to the application using Devise.

    Before I switched to the Postgresql database I ran my developement.sqlite3 file through a database converter -- this is something that I've done in the past with no problems.

    I'm unsure of how to debug this application as these to_sym errors are occuring all throughout.

    To give you an example here's one of the pages that stopped working:

    NoMethodError in Home#index

    Showing C:/Users/chunter33/Desktop/devise_forem/app/views/layouts/application.html.haml where line #19 raised:

    undefined method `to_sym' for nil:NilClass

  • 16: %a{:href => "#"}= t("web-app-theme.profile", :default => "Profile")
  • 17: %li
  • 18: %a{:href => "#"}= t("web-app-theme.settings", :default => "Settings")
  • 19: - if signed_in?
  • 20: %li
  • 21: %a
  • 22: = current_user.email
  • Application Trace:

    app/helpers/application_helper.rb:3:in `signed_in?'

    app/views/layouts/application.html.haml:19:in
    `_app_views_layouts_application_html_haml__944875441_29833656'

    Note: If I switch back to SQLite I no longer get any of these errors.

    I'm using Ruby 1.8.7 and Rails 3.1

    Any ideas where I can start looking?

    Let me know if there's any additional information that you need.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    猫卆 2024-12-20 14:51:04

    我不懂ruby,但我知道PostgreSQL,这与数据库无关。

    您的两个相关错误是:

    1. NoMethodError in Home#index
    2. undefined method `to_sym' for nil:NilClass

    您有一个 nil/null 对象,它试图调用“to_sym”。模板中的某些变量未定义。

    由于您更改了数据库,我怀疑:

    1. 与数据库的连接失败。
    2. 当您期望返回一些行时,查询却没有返回任何行。

    这很难说,但更多的错误检查和一些日志记录应该会有所帮助。

    I don't know ruby, but I do know PostgreSQL and this is nothing to do with the database.

    Your two relevant errors are:

    1. NoMethodError in Home#index
    2. undefined method `to_sym' for nil:NilClass

    You've got a nil/null object it's trying to call "to_sym" on. Some variable in your template is undefined.

    Since you've changed the db, I'd suspect either:

    1. The connection to the database failed.
    2. A query has returned no rows when you were expecting some.

    Which is difficult to say, but more error checking and a bit of logging should help.

    以往的大感动 2024-12-20 14:51:04

    我发现了问题的根源。我使用 ESF 数据库迁移工具包将数据从 sqlite3 迁移到 Postgresql。这样做的问题是,它无法正确处理从 sqlite3 到 PostgreSQL 的转换,并且 Rails 生成的 id 列的自动递增性质在 PostgreSQL 中无法像在 Sqlite3 中那样工作。为了解决这个问题,我只需要删除数据库中的所有表,将数据库版本重置为 0 并运行 rake db:migrate,同时将我的 database.yml 设置为从 PostgreSQL 数据库运行。

    I discovered the root of the problem. I used the ESF Database Migration toolkit to migrate my data from sqlite3 to Postgresql. The problem with this is that it doesn't handle the conversion from sqlite3 to PostgreSQL properly and the auto incrementing nature of the rails generated id column doesn't work in PostgreSQL as it does in Sqlite3. To fix this I simply needed to drop all the tables in my database, reset the database version to 0 and run a rake db:migrate while my database.yml is set to run off of the PostgreSQL database.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文