我的布局/ application.html.erb上有一个表单,发布在下面,如果我添加行<%= devise_error_messages! %>
,我会收到以下错误。
如何在没有分页符的情况下添加devise_error_messages?
/app/views/layouts/application.html.erb
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %> <!-- this line causes issues -->
<h3>Add new contact</h3>
First Name<br />
<%= f.text_field :username %><br />
Last Name<br />
<%= f.text_field :password %><br />
<%= f.text_field :password_confirmation %><br />
Email<br />
<%= f.text_field :email %>
<hr />
<%= f.submit "Add Contact" %>
<% end %>
错误:
NoMethodError in My_devise/sessions#index
Showing /app/views/layouts/application.html.erb where line #59 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #59):
56: <a href="#" class="has-popupballoon button button-blue"><span class="add"></span>New Contact</a>
57: <div class="popupballoon top">
58: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
59: <%= devise_error_messages! %>
60: <h3>Add new contact</h3>
61: First Name<br />
62: <%= f.text_field :username %><br />
这是我使用http://localhost:3000/admin/home
的网址,这是我的路线.rb
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
get "/users/sign_in", :to => "my_devise/sessions#new"
get 'admin/home', :to => 'my_devise/sessions#index'
get 'users/sign_up', :to => 'my_devise/registrations#new'
end
devise_for :users, :controllers => {:sessions => "my_devise/sessions", :registrations => "my_devise/registrations"}
get "home/index"
root :to => "home#index"
/app/controllers/my_devise/sessions_controller.rb
class MyDevise::SessionsController < Devise::SessionsController
layout false
before_filter :authenticate_user!
def index
render :layout => 'application'
end
def new
super
end
def create
super
end
end
/app/controllers/my_devise/registrations_controller.rb
class MyDevise::RegistrationsController < Devise::RegistrationsController
def index
end
end
耙路线:
users_sign_out GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
users_sign_in GET /users/sign_in(.:format) {:controller=>"my_devise/sessions", :action=>"new"}
admin_home GET /admin/home(.:format) {:controller=>"my_devise/sessions", :action=>"index"}
users_sign_up GET /users/sign_up(.:format) {:controller=>"my_devise/registrations", :action=>"new"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"my_devise/sessions"}
POST /users/sign_in(.:format) {:action=>"create", :controller=>"my_devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"my_devise/sessions"}
POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
GET /users/cancel(.:format) {:action=>"cancel", :controller=>"my_devise/registrations"}
POST /users(.:format) {:action=>"create", :controller=>"my_devise/registrations"}
GET /users/sign_up(.:format) {:action=>"new", :controller=>"my_devise/registrations"}
GET /users/edit(.:format) {:action=>"edit", :controller=>"my_devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"my_devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"my_devise/registrations"}
home_index GET /home/index(.:format) {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
答案 0 :(得分:2)
我遇到了类似的问题。
花了我一段时间来弄明白,但就我而言,原因是:
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
在我的application_controller.rb文件中。
我在教程中添加了这一行。删除后,我没有收到有关“devise_error_messages”的错误。我不太明白为什么这会让人感到不安,也许'current_user'在别处被定义,它会设置(或导致设置)错误对象。
答案 1 :(得分:0)
尝试更改devise_for路由,请查看此文档http://rdoc.info/github/plataformatec/devise#Configuring_routes
您可以使用:path参数
添加自己的前缀这是一个示例
devise_for :users, :path => "", :path_names => {
:sign_in => 'signin',
:sign_out => 'signout',
:sign_up => 'signup'
},
:sign_out_via => [:delete],
:controllers => {:sessions => 'my_devise/sessions', :registrations =>
'my_devise/registrations'}
答案 2 :(得分:0)
重新审视一个项目我暂时没有时间触摸,但简单的答案就是在application_controller中创建User对象。
我将此代码段添加到application_controller:
before_filter :instantiateUser
def instantiateUser
@user = User.new
end