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">× </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
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
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With