【问题标题】:Devise throw ArgumentError in rails 3.1在rails 3.1中设计抛出ArgumentError
【发布时间】:2011-11-30 08:17:21
【问题描述】:

在 Rails 3.1 应用程序和更新的 Devise 1.4.7 中,当我访问 http://localhost:3000/users/sign_up(如 rake 路由中所示)时,我得到“ArgumentError in Devise/registrations#new”提取的源代码是行3:

<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do  |f| %>.

解决办法是什么?提前谢谢你。

Deals::Application.routes.draw do
  devise_for :users
  root :to => "home#index"
end

【问题讨论】:

    标签: ruby-on-rails-3 devise ruby-on-rails-3.1


    【解决方案1】:

    我可以确认,当您运行 rails generate devise:views 并查看从 gem 复制到 app/views/devise/registrations/new.html.erb 的工作默认模板时,form_for 是被 Devise 调用并带有参数:

    form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
    

    如果您希望参考复制 gem 视图,请查看 the views in the repo

    但是,如果您(或阅读本文的其他人)尝试将注册表单与您自己的控制器一起使用,那么您完全有可能需要重新创建一些方法 from the Devise controller 才能使用 get the form to render。假设你调用了你的用户类User

    def resource_name
      :user
    end
    
    def resource
      @resource ||= User.new
    end
    
    def devise_mapping
      @devise_mapping ||= Devise.mappings[:user]
    end
    

    【讨论】:

      【解决方案2】:

      试试这个。

      <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
      

      【讨论】:

        【解决方案3】:

        你的论点太多了。 form_for 只需要一个参数(资源),像这样:

        @registration = Registration.new
        form_for @registration
        

        如果需要,您可以选择传入:url

        【讨论】:

          猜你喜欢
          • 2012-09-24
          • 2014-10-20
          • 1970-01-01
          • 2011-11-18
          • 1970-01-01
          • 1970-01-01
          • 2011-12-25
          • 1970-01-01
          • 2011-12-18
          相关资源
          最近更新 更多