开发者

Devise throw ArgumentError in rails 3.1

开发者 https://www.devze.com 2023-04-11 01:31 出处:网络
In Rails 3.1 app and updated Devise 1.4.7, when I visit http://localhost:3000/users/sign_up (as indicated in rake routes), I get \"ArgumentError in Devise/registrations#new\" extracted开发者_JAVA技巧

In Rails 3.1 app and updated Devise 1.4.7, when I visit http://localhost:3000/users/sign_up (as indicated in rake routes), I get "ArgumentError in Devise/registrations#new" extracted开发者_JAVA技巧 source is line 3:

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

What is the solution to this? Thank you in advance.

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


You have one too many arguments there. form_for only needs one argument (the resource), like this:

@registration = Registration.new
form_for @registration

And you can optionally pass in :url if you need to.


Try this one.

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


I can confirm that when you run rails generate devise:views and look inside the working default template copied from the gem to app/views/devise/registrations/new.html.erb, form_for is being called by Devise with the arguments:

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

If you would prefer a reference to copying the gem views, check out the views in the repo.

However if you (or someone else reading this) try to use the signup form with your own controller, it is entirely possible that you will need recreate some of the methods from the Devise controller in order to get the form to render. Assuming you called your user class User:

def resource_name
  :user
end

def resource
  @resource ||= User.new
end

def devise_mapping
  @devise_mapping ||= Devise.mappings[:user]
end
0

精彩评论

暂无评论...
验证码 换一张
取 消