简体   繁体   English

Rails Controller代码未执行

[英]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.0 Rails 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.

相关问题 通过链接访问页面时,Rails JavaScript代码未执行 - Rails JavaScript code isn't executing when page is accessed with link Rails视图没有将数据传递给控制器 - Rails view isn't passing the data to the controller Rails controller 没有获得特定的参数 - Rails controller isn't getting specific params 在Rails控制器中执行代码之前渲染状态200 - Render status 200 before executing code in rails controller Rails-控制器中的Redirect_to错误-“页面未正确重定向” - Rails - Redirect_to error in controller - “The page isn't redirecting properly” " Rails控制器操作未正确执行 - rails controller actions not executing properly 在 Rails 中:从与模型无关的表单中检索用户输入,在控制器中使用结果 - in Rails : Retrieve user input from a form that isn't associated with a Model, use the result within the controller 在会话控制器在 rails 中销毁它之后,为什么 current_account 不是零? - Why isn't current_account nil, after the session controller destroys it in rails? “ static_pages#home”无法访问CarrierWave的rails图像上载器的控制器 - CarrierWave's rails image uploader's controller isn't accessible to 'static_pages#home' 为什么我的rails控制器/表单不将任何数据持久保存到本地数据库? - Why isn't my rails controller/form persisting any data to my local database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM