[英]Rails Controller code isn't executing
I must be missing something... my views are being rendered, but it appears like code in the controller isn't being executed. 我必须缺少一些东西...我的视图正在渲染,但看起来好像未执行控制器中的代码。 I can't find what's wrong.
我找不到问题所在。
When I click the login button in login.html.erb
, the login_process
method of the PublicController should be called to authenticate the user. 当我单击
login.html.erb
的登录按钮时,应该调用PublicController的login_process
方法来对用户进行身份验证。 That doesn't seem to happen as the view login_process.html.erb
just displays, which shouldn't be possible. 这似乎没有发生,因为只显示了视图
login_process.html.erb
,这是不可能的。
I've even tried putting a flash[:notice] = "heyhey"
on the login_screen
action so it appears on the login form, but that doesn't happen either. 我什至尝试将
flash[:notice] = "heyhey"
login_screen
flash[:notice] = "heyhey"
放在login_screen
操作上,以便它出现在登录表单上,但这也不会发生。
What have I done wrong? 我做错了什么? Is it something in the
routes.rb
? 在
routes.rb
有东西吗?
routes.rb routes.rb
root :to => "public#index"
get "login",
:to => "public#login",
:as => "login_screen"
post "login_process",
:to => "public#login_process",
:as => "login_process"
get "logout",
:to => "public#logout",
:as => "logout"
public_controller.rb public_controller.rb
class PublicController < ApplicationController
def login_screen
flash[:notice] = "heyhey"
end
def login_process
authenticated_user = User.authenticate params[:email], params[:password]
if authenticated_user
session[:user_id] = authenticated_user.id
session.countdown_start(1.minute)
redirect_to :public => :index
else
flash[:notice] = 'Email/Password incorrect. <a href="#">Receive a new password</a>.'
flash[:color] = "invalid"
end
render :login_screen
end
def logout
session.countdown_abort
flash[:notice] = "You are now logged out"
redirect_to :login_screen
end
def count
render :text => session.countdown_count.to_i
end
end
views/public/login.html.erb views / public / login.html.erb
<h1>Login</h1>
<p id="notice"><%= notice %></p>
<%= form_tag :login_process do %>
<div class="field">
<%= label_tag :email %><br>
<%= text_field_tag :email %>
</div>
<div class="field">
<%= label_tag :password %><br>
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag "Login" %>
</div>
<% end %>
<h1>Login</h1>
<p id="notice"><%= notice %></p>
<%= form_tag :login_process do %>
<div class="field">
<%= label_tag :email %><br>
<%= text_field_tag :email %>
</div>
<div class="field">
<%= label_tag :password %><br>
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag "Login" %>
</div>
<% end %>
login_process.html.erb login_process.html.erb
I never expect to see this page as the user should be directed elsewhere 我从没希望看到此页面,因为应该将用户定向到其他地方
<p>login_process.html.erb</p>
<p id="notice"><%= notice %></p>
Update 1: 更新1:
I've modified the
authenticate
action in the User model to be:我已将用户模型中的
authenticate
操作修改为:def authenticate(:email, :password) #x = new User #x.id = 123 return false #x end
and
login_process
on PublicController to be:和
login_process
上的login_process为:def login_process if true flash[:notice] = 'boom' else flash[:notice] = 'Email/Password incorrect. <a href="#">Receive a new password</a>.' flash[:color] = "invalid" end redirect_to :login_screen end
Unfortunately though... same issue.
不幸的是...同样的问题。 The
login_process.html.erb
is displayed with no flash notices either.显示的
login_process.html.erb
也没有任何闪动通知。Versions:
版本:
Ruby 2.0.0p247红宝石2.0.0p247
Rails 4.0.0Rails 4.0.0
The code in your controller is probably executing just fine. 控制器中的代码可能正在执行。
I think the authenticate
class method on User
is not giving you the correct result. 我认为
User
上的authenticate
类方法无法为您提供正确的结果。 That's why it doesn't go through the if authenticated_user
and just renders the login_process
view. 这就是为什么它不通过
if authenticated_user
而是仅呈现login_process
视图的原因。 You don't have to call this render command though, it's done for you if you do nothing. 不过,您不必调用此render命令,如果您什么都不做就可以为您完成。
There's a problem with the login_process
method though. 但是
login_process
方法存在问题。 You can't have a render call and a redirect_to be called together, now it can happen in your case, if the user is authenticated. 您无法同时调用render调用和redirect_to,如果您的用户通过了身份验证,则可能会发生这种情况。 Put the
render :login_screen
in the else case, otherwise you get the AbstractController::DoubleRenderError
error saying: Render and/or redirect were called multiple times in this action. [etc.]
将
render :login_screen
放在else情况下,否则会出现AbstractController::DoubleRenderError
错误消息: Render and/or redirect were called multiple times in this action. [etc.]
Render and/or redirect were called multiple times in this action. [etc.]
This is what you would get as an error if the user was authenticated. Render and/or redirect were called multiple times in this action. [etc.]
如果用户通过了身份验证,这就是错误。
Edit: Running this locally (stubbing User.authenticate
) on a new Rails project it works. 编辑:在新的Rails项目上运行该文件(在本地运行
User.authenticate
)。 Flash notices appear and everything. 出现Flash通知,一切都会出现。 Also:
render :login_screen
is probably render :login
, no? 另外:
render :login_screen
大概是render :login
,不是吗? in login_process? 在login_process中?
If nothing works still, what's your: Ruby version, Rails version, put authenticated_user = false # or true
in login_process to see if it behaves differently. 如果仍然无法正常工作,那么您的情况是什么:Ruby版本,Rails版本,在login_process中放置
authenticated_user = false # or true
来查看其行为是否不同。
redirect_to :public => :index
It seems like you are redirecting from login_process to /login_process?public=index, which I don't think is your intention. 似乎您正在从login_process重定向到/ login_process?public = index,我认为这不是您的意图。
If you are meaning to redirect to public#index, try 如果您要重定向到public#index,请尝试
redirect_to root_path
(But I can't see that action in your public controller.) (但我无法在您的公共控制器中看到该操作。)
I believe the problem is that you call two renders in one action. 我认为问题在于您一次执行两个渲染。 You can only render one page per action.
每个动作只能呈现一页。
def login_process
authenticated_user = User.authenticate params[:email], params[:password]
if authenticated_user
session[:user_id] = authenticated_user.id
session.countdown_start(1.minute)
redirect_to :public => :index
else
flash[:notice] = 'Email/Password incorrect. <a href="#">Receive a new password</a>.'
flash[:color] = "invalid"
end
render :login_screen
end
The user is authenticated and the redirect_to should bring you to the public/index, however, you call render :login_screen
after the if statement, so it will execute that portion of the code no matter what and bring you back to the login. 用户已通过身份验证,redirect_to应该将您带到公共目录/索引,但是,您在if语句之后调用
render :login_screen
,因此无论执行什么操作,它将执行该部分代码,并使您重新登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.