【问题标题】:Rails 4 - Devise 3 not working (NoMethodError in Users::RegistrationController#create )Rails 4 - 设计 3 不起作用(Users::RegistrationController#create 中的 NoMethodError )
【发布时间】:2015-01-26 03:02:50
【问题描述】:

Devise 无法像以前在 Rails 4 中那样工作。我没有更改用户模型中的任何内容。它看起来像这样 -

create_table(:users) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  t.timestamps
end

add_index :users, :email,                :unique => true
add_index :users, :reset_password_token, :unique => true

我的路线设计看起来像这样 -

devise_for :users, path: "", controllers: {
                   sessions: "users/session",
                   registrations: "users/registration",
                   passwords: "users/password"
               }, path_names: {
                   sign_in: 'login',
                   password: 'forgot',
                   confirmation: 'confirm',
                   unlock: 'unblock',
                   sign_up: 'register',
                   sign_out: 'logout'
               }

当我尝试从默认注册页面(即registrations/new.html.erb)注册时,我收到以下错误 -

Users::RegistrationController#create 中的 NoMethodError

#Array:0x007f8b33bcd750 的未定义方法“deep_symbolize_keys”

我的注册表看起来像这样(非常基本)-

<%= form_for(resource, :as => resource_name, :url => user_registration_path(resource_name)) do |f| %>
  <fieldset>
    <legend><h5 class="color1">Sign up</h5></legend>

    <%= devise_error_messages! %>

    <%= f.text_field :first_name, :placeholder => "Your first name", :autofocus => true %>

    <%= f.text_field :last_name, :placeholder => "Your last name" %>

    <%= f.text_field :email, :placeholder => "Your email address" %>

    <%= f.phone_field :phone_number, :placeholder => "Your phone number (Optional)" %>

    <%= f.password_field :password, :placeholder => "Your password" %>

    <%= f.password_field :password_confirmation, :placeholder => "Please confirm your password" %>

    <%= content_tag :button, :type => :submit, :class => "button small success" do %>
       Sign up
      <% end %>

 </fieldset>
<% end %>

我尝试过的解决方案:

  1. 我尝试更新所有 gem(包括目前为 3.4 的 Devise,以前是 3.0),但仍然没有成功
  2. 删除了数据库,重新创建并运行了所有迁移,没有运气
  3. 试图创建一个新的控制器并从 Devise::RegistrationsController 继承,但没有成功

编辑:我的控制器代码如下所示 -

class Users::RegistrationController < Devise::RegistrationsController
end

application.rb 看起来像这样 -

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module AppName
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
  end
end

我错过了什么?任何帮助将不胜感激!

抱歉这个问题太长了!

【问题讨论】:

  • 显示控制器代码
  • 显示文件 config/application.rb 和 application_controller.rb
  • 那个地方应该是散列,而不是数组。查看堆栈跟踪并设计来源以找到问题,它们很简单。
  • 谢谢亚历克斯,它相当简单和愚蠢。我不知道为什么错误不明显,我必须通过完整的跟踪才能看到 i18n 问题。感谢您指出。

标签: ruby-on-rails ruby-on-rails-4 devise


【解决方案1】:

好的,我发现了怎么回事。

正如 Alex 所建议的,我注意到错误发生在 i18n store_translations 方法中。

我在谷歌上搜索了一下,发现了这个 - https://github.com/plataformatec/devise/issues/337

正如 Steven 在那个 github 线程中指出的那样,报告这个问题的人在 config/locales 下有一些奇怪的 yml,我也是。

我删除了yml,它似乎工作得很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多