ruby on rails - Test if error validation text match a string using if statement doesn't works -


i have validation in user model check input text if have value or not :

validate :picture_name ... ... ...  def picture_name     errors[:picture].push "no_name" if namepicture.blank? end 

html text field:

<input type="text" name="namepicture" /> 

in view want check if @user.errors[:picture] contains 1 error , error text "no_name" (no_name error added picture_name validation) :

<% if @user.errors.count.eql? 1 && @user.errors['picture'] == "no_name" %>    <h1> test works </h1> <% else %>    <h1> test doesn't works </h1> <% end %> 

but don't know why shows me "test doesn't works" when errors['picture'] contains (one) "no_name" error !

there error in if statement ? or ?

thank you

i found solution if interest someone, in above statement :

if @user.errors.count.eql? 1 && @user.errors['picture'] == "no_name" 

i use && operator high precedence, means statement :

if @user.errors.count.eql? ( 1 && @user.errors['picture'] == "no_name" ) 

or more explicitly

if @user.errors.count.eql? true 

which incorrect

so, solution :

using parentheses :

if (@user.errors.count.eql? 1) && (@user.errors['picture'] == "no_name") 

or using "and" operator lower precedence:

if @user.errors.count.eql? 1 , @user.errors['picture'] == "no_name" 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -