Ruby on rails Rails:跨控制器复制功能?卑微的请求
因此,我正在使用authlogic,并尝试将登录功能复制到欢迎页面,这样您就可以通过restful url登录,或者直接转到主页。不,我不知道我们是否会保留该功能,但我还是想测试一下。以下是错误消息: 欢迎索引中的Ruby on rails Rails:跨控制器复制功能?卑微的请求,ruby-on-rails,Ruby On Rails,因此,我正在使用authlogic,并尝试将登录功能复制到欢迎页面,这样您就可以通过restful url登录,或者直接转到主页。不,我不知道我们是否会保留该功能,但我还是想测试一下。以下是错误消息: 欢迎索引中的RuntimeError 为nil调用id,它会错误地为4——如果您真的想要nil的id,请使用object_id 代码如下。基本上,发生的是索引视图(第一个代码段)将信息从表单发送到用户会话控制器的create方法。从理论上讲,在这一点上,它应该会上升,但它不会 请帮忙。请我已经做了
RuntimeError
为nil调用id,它会错误地为4——如果您真的想要nil的id,请使用object_id
代码如下。基本上,发生的是索引视图(第一个代码段)将信息从表单发送到用户会话控制器的create方法。从理论上讲,在这一点上,它应该会上升,但它不会
请帮忙。请我已经做了8个小时了。我查了谷歌。我查了IRC。我检查了我能找到的每一本书。你甚至不必回答,只要你给我指出正确的方向,我就可以开始繁重的工作
==编辑===
Sameera很好地回答了这个问题。不过,还有一个悬而未决的问题,那就是组织应用程序的最佳方式是什么。在before_过滤器中应用object@user_会话是可以接受的,还是有一种更适合rails的方法
欢迎#索引
user_sessions_path do|f|%>
应用程序控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
helper_method :current_user_session, :current_user
before_filter :new_session_object
protected
def new_session_object
unless current_user
@user_session = UserSession.new(params[:user_session])
end
end
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end<pre></code>
1: <h1>Welcome#index</h1>
2: <p>Find me in app/views/welcome/index.html.erb</p>
3:
4: <% form_for @user_session, :url => user_sessions_path do |f| %>
5: <%= f.text_field :email %><br />
6: <%= f.password_field :password %>
7: <%= submit_tag 'Login' %>
Application Trace | Framework Trace | Full Trace
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpac -2.3.5/lib/action_controller/record_identifier.rb:76:in `dom_id'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/record_identification_helper.rb:16:in `dom_id'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:293:in `apply_form_for_options!'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/Users/alex/Desktop/anglic/app/views/welcome/index.html.erb:4:in `_run_erb_app47views47welcome47index46html46erb'
class ApplicationController
用户会话控制器
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
helper_method :current_user_session, :current_user
before_filter :new_session_object
protected
def new_session_object
unless current_user
@user_session = UserSession.new(params[:user_session])
end
end
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end<pre></code>
1: <h1>Welcome#index</h1>
2: <p>Find me in app/views/welcome/index.html.erb</p>
3:
4: <% form_for @user_session, :url => user_sessions_path do |f| %>
5: <%= f.text_field :email %><br />
6: <%= f.password_field :password %>
7: <%= submit_tag 'Login' %>
Application Trace | Framework Trace | Full Trace
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpac -2.3.5/lib/action_controller/record_identifier.rb:76:in `dom_id'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/record_identification_helper.rb:16:in `dom_id'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:293:in `apply_form_for_options!'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/Users/alex/Desktop/anglic/app/views/welcome/index.html.erb:4:in `_run_erb_app47views47welcome47index46html46erb'
class UserSessionController'user\u sessions',:操作=>'new'
结束
结束
def销毁
@user\u session=UserSession.find
@用户会话.destroy
flash[:注意]=“注销”
将\u重定向到根\u url
结束
结束
更详细的堆栈跟踪
class WelcomeController < ActionController::Base
def index
@user_session = UserSession.new
.....
end
.....
end
1:欢迎#索引
2:在app/views/welcome/index.html.erb中查找我
三:
4:用户会话路径do | f |%>
5:
6:
7:
应用程序跟踪|框架跟踪|完整跟踪
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/Ruby/gems/1.8/gems/actionpac-2.3.5/lib/action\u controller/record\u identifier.rb:76:在'dom\u id'中
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/Ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\u view/helpers/record\u identification\u helper.rb:16:in'dom\u id'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/Ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\u view/helpers/form\u helper.rb:293:in“apply\u form\u for\u options!”
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/Ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\u view/helpers/form\u helper.rb:277:in'form\u for'
/Users/alex/Desktop/anglic/app/views/welcome/index.html.erb:4:在“运行”中
我想你可以在你的WelcomeController中尝试一下
class ApplicationController < ActionController::Base
.....
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
.....
end
使用params[:user\u session]
创建UserSession.new
的任何原因
据我所知,UserSessionController
登录的流程是:
:调用新的会话对象
false
for
当前用户
,因为它是新用户
用户会话@user\u session
变量
到
UserSession.new(参数[:user\u session])
价值观UserSessionController#新建
操作
被称为哪个覆盖
@user\u session
变量到
UserSession.new
new.html.erb
被呈现
包含用于登录的表单在\u筛选器之前:新建\u会话\u对象
是否再次调用
false
for
当前用户
,因为它仍然是
新用户会话@user\u session
变量
到
UserSession.new(参数[:user\u session])
价值观UserSessionController#创建
行动将再次被调用
将@user\u session
变量设置为
UserSession.new(参数[:user\u session])
价值观@user\u会话
已保存,并且
用户会话已成功完成
创造WelcomeController
来说,在第4步中它有细微的不同
WelcomeController#索引中使用的@user_session
变量值来自前过滤器调用UserSession.new(params[:user_session])
的变量分配,此时参数[:user_session]
值为nil
,因此,当呈现index.html.erb
时,@user\u session=UserSession.new(nil)
我不完全确定这是否是导致您出现问题的原因,但如果您在使用WelcomeController
时运行script/server--debugger
并逐步创建@user\u session
,特别是当表单I
protected
def new_session_object
unless current_user
@user_session = UserSession.new(params[:user_session])
end
end
def require_user
unless current_user
flash[:notice] = "You must be logged in to access this page!"
redirect_to :controller => "user_sessions", :action => "new"
return false
end
end
before_filter :require_user
form_for :user_session, @user_session, :url => user_session_path(@user_session)} do |f|
end