简体   繁体   English

无效登录后,Ruby on Rails向用户返回一条消息

[英]Ruby on Rails return a message to the user after invalid login

I am new at ruby on rails and I am trying to set up a simple log in for a part of the site. 我是ruby on rails的新手,我正在尝试为该网站的一部分设置一个简单的登录信息。 I have gotten the log in to work however when the user puts in a incorrect username, I want a message to say "Invalid log in. Try again." 我已经登录成功,但是当用户输入错误的用户名时,我希望显示一条消息“无效的登录。请重试。” I can not figure this out any help would be greatly appreciated 我不知道这有什么帮助,将不胜感激

HERE IS MY CODE IN MY CONTROLLER 这是我的控制器中的代码

class SimpleloginController < ApplicationController  
  def namein    

  end

  def transmain  
    @familyname_out = params[:family_name_in] #textbox name from the input file 
    name_out  = params[:family_name_in]   
    if found = Family.find_by_name(name_out)   
      redirect_to manin_manout_path, :notice => "Logged in successfully" 
    else  
      redirect_to simplelogin_namein_path, :notice => "Invalid login. Try again" 
    end    
  end 
end   
def transmain   
  unless Family.find_by_name(params[:family_name_in]).blank?
    redirect_to manin_manout_path, :notice => "Logged in successfully" 
  else  
    redirect_to simplelogin_namein_path, :notice => "Invalid login. Try again" 
  end    
end 

Are you rendering the notice messages in your view? 您是否在视图中呈现通知消息?

<% flash.each do |name, msg| %>
  <%= msg %>
<% end %>

I'm not sure about the behavior you're experiencing, but I think this line 我不确定您的行为,但是我认为这句话

if found = Family.find_by_name(name_out)

will return a RecordNotFound error if name_out doesn't exist in the db. 如果name_out在数据库中不存在,将返回RecordNotFound错误。

For a good look at authentication with rails check out Ryan Bates' Railcast Authentication from Scratch (Revised) 想要更好地了解使用Rails进行身份验证,请从头开始阅读Ryan Bates的Railcast Authentication(修订版)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM