简体   繁体   English

Ruby on Rails在控制器上获取验证错误消息

[英]Ruby on Rails get validation error messages on controller

I am new to Ruby on Rails and new to this site. 我是Ruby on Rails的新手,也是本站点的新手。 I am trying to get on my controller the errors from the validations I set on my model so I can change a flash message depending if this errors occurred or not. 我试图在控制器上获取我在模型上设置的验证中的错误,以便根据是否发生此错误来更改闪存消息。

I have this validations: 我有这个验证:

class User < ActiveRecord::Base
attr_accessible :login, :password
validates_presence_of :login , :message => 'Login can't be empty'
validates_presence_of :password ,:message => 'Password can't be empty'

I have this code in my controller: 我的控制器中有以下代码:

def create
login = params[:username].downcase
password = params[:password]
tuser=User.new(login: login, password: password).save
if(tuser.errors.any?)
  flash[:title]='¡Error!'
  flash[:notice]='Please verify your data'
else
  flash[:title]='¡Success!'
  flash[:notice]='Your account was created'
end

And I am using this flash objects on my html to render a bootstrap modal: 我在html上使用了以下Flash对象来呈现引导程序模式:

<% if flash[:notice] %>
<div class="modal hide fade" id="modal">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;    </button>
    <h3><%= flash[:title]  %></h3>
  </div>
  <div class="modal-body">
    <%= flash[:notice]  %>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
  </div>
</div>

The problem occurs in the controller and I get this error: 该问题发生在控制器中,并且出现此错误:

NoMethodError in UserController#create

undefined method `errors' for false:FalseClass

I want to change my flash [notice] error message to the ones thrown by the validations, but I can't do it. 我想将我的flash [notice]错误消息更改为验证所引发的错误消息,但是我不能这样做。 I have followed some tutorials but they use the @tuser global variable on the .html file to get the validation error messages and I need to handle them on the contoller. 我遵循了一些教程,但是它们使用.html文件上的@tuser全局变量来获取验证错误消息,我需要在contoller上进行处理。 ¿Can anyone help me? 谁能帮我?

Thanks 谢谢

When you call save it validates and then returns True if the save was successful or False if it was not so you were setting tuser to True or False when you called save. 当您调用保存时,它将进行验证,如果保存成功,则返回True否则,返回False ,因此您在调用save时将tuser设置为TrueFalse If Save is false the errors are generated and accessible so there is no need to check for them. 如果Save为false,则会生成并可以访问错误,因此无需检查它们。 Try this: 尝试这个:

def create
  login = params[:username].downcase
  password = params[:password]
  @tuser=User.new(login: login, password: password)
  #you could also use @tuser = User.new(params[:user]) but you will have to rename :username to :login
  if tuser.save
    flash[:title]='¡Success!'
    flash[:notice]='Your account was created'
    #redirect_to user_path(@tuser)
  else
    flash[:title]='¡Error!'
    flash[:notice]='Please verify your data'
    #render 'new'
  end
end

You should probably also add a redirect or render to the code as above commented out since I do not know where you are calling this from. 您可能还应该将重定向或渲染添加到上面注释掉的代码中,因为我不知道您从何处调用它。

Edit Also adding the downcase to the model would be more inline with Rails eg 编辑另外,将小写字母添加到模型中将与Rails更加内联,例如

before_save {|user| user.login = login.downcase}

try this out: 试试看:

Active record save returns either true or false, try using create instead, which returns object itself. 活动记录save返回true或false,请尝试使用create来返回对象本身。

def create
  login = params[:username].downcase
  password = params[:password]
  tuser = User.create(login: login, password: password)
  if(tuser.errors.any?)
    flash[:title]='¡Error!'
    flash[:notice]='Please verify your data'
  else
    flash[:title]='¡Success!'
    flash[:notice]='Your account was created'
  end
end

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

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