问题描述
因此,我试图做与
完全
我有一个辅助模块,该模块具有
I have a helper module that has
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
但是我得到的是未定义的方法'[]'对于行上的nil:NilClass
<%= f.email_field :email, :autofocus => true %></div>
我在视图/设计/会话/new.html中将<%提高resource.inspect%> .erb
I put <% raise resource.inspect %> in views/devise/sessions/new.html.erb
,而我在Home#index #User not initialized
and I got RuntimeError in Home#index #User not initialized
<h2>Sign in</h2>
<%= form_for(resource, :as => resource_name, :url => user_session_path(resource_name)) do |f| %>
<% raise resource.inspect %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
<% end %>
<%= render "devise/shared/links" %>
我也为辅助程序模块尝试过此操作
and I also tried this for the helper module
def resource_name
:user
end
def resource
@resource = User.new
end
def devise_mapping
@devise_mapping = Devise.mappings[:user]
end
在Home#index中,我有以下
In Home#index, I have the following
<div class="loginform" id="login">
<%= render :file => "devise/sessions/new" %>
</div>
如果视图/设计错误,错误为nil:NilClass的未定义方法'[]'将消失/sessions/new.html.erb编辑为
The error "undefined method `[]' for nil:NilClass" will be gone if views/devise/sessions/new.html.erb edits to be
<%= form_for(:user, :as => @user, :url => user_session_path(@user)) do |f| %>
,但如果放置<%提高资源。检查%>,则返回视图/设计/会话/新.html.erb
but if put <% raise resource.inspect %> back in views/devise/sessions/new.html.erb
仍会得到#User未初始化>
will still get #User not initialized>
在user.rb
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
在application_controller.rb
In application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper :all
end
使用'rails','3.2.11'
using 'rails', '3.2.11'
我不确定为什么辅助模块不能像它预期的那样工作。谁能帮助我了解原因?以及为什么要为nil:NilClass获取未定义的方法'[]'?
I am not sure why the helper module doesn't work the way it supposes be for resource. Can anyone help me to understand why? and why getting "undefined method `[]' for nil:NilClass" ?
谢谢。
推荐答案
我认为这行
<%= form_for(:user, :as => @user, :url => user_session_path(@user)) do |f| %>
应该是
<%= form_for(@user, :as => :user, :url => user_session_path(@user)) do |f| %>
这篇关于在不同视图中设计表单“ nil:NilClass的未定义方法'[]'”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!