Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails get validation error messages on controller

I am new to Ruby on Rails and new to this site. 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:

<% 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. 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. ¿Can anyone help me?

Thanks

like image 782
Diego Serrano Avatar asked Sep 24 '13 16:09

Diego Serrano


2 Answers

try this out:

Active record save returns either true or false, try using create instead, which returns object itself.

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
like image 31
Sachin Singh Avatar answered Oct 19 '22 13:10

Sachin Singh


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. If Save is false the errors are generated and accessible so there is no need to check for them. 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 e.g.

before_save {|user| user.login = login.downcase}
like image 191
engineersmnky Avatar answered Oct 19 '22 13:10

engineersmnky