Hi
I am making an application using ruby on rails. For user sign up I have
given following validation rules
class User < ActiveRecord::Base
validates_presence_of :first_name, :on => :create, :message => “First
Name is mandatory”
validates_presence_of :last_name, :on => :create, :message => “Last
Name is mandatory”
validates_presence_of :login_name, :on => :create, :message =>
“Login Name is mandatory”
validates_presence_of :password, :on => :create, :message =>
“Password is mandatory”
validates_presence_of :email, :on => :create, :message => “Email is
mandatory”
validates_uniqueness_of :login_name, :on => :create, :message =>
“user already exists”
validates_format_of :login_name, :with => /\w/, :on => :create,
:message => “Invalid character used for Login Name”
validates_confirmation_of :password, :on => :create, :message =>
“password don’t match”
validates_length_of :password, :within => 5…40, :message => “No. of
characters in password is out of limit”
validates_format_of :email, :with =>
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/, :on => :create, :message =>
“Wrong Format of Email ID”
end
in the view file for sign up i am using both functions:
error_messages_for ‘user’
and
error_message_on “user”, “field_name”
I found that while attempting to create new object rails are checking
all above conditions and it does not allow to create object if any of
the condition fails but its not displaying the error messages.
I tried to find, whats wrong but all in vain…
Please help me if i made mistake in the syntax.
Thanks in advance
Nitin
Hi Nitin M.
Please paste your form for new.html.erb
Sijo
Sijo k g wrote:
Hi Nitin M.
Please paste your form for new.html.erb
Sijo
Hi Sijo,
Thanks for replying. Here is the template to add new user:
Please fill the following details:
<%= error_messages_for 'user' %>
'*' fields are mandatory
Sign up for an account |
<%= form_tag :controller => "user",
:action => "signup" %>
<p><font color="red">*</font> First Name: <%= text_field "user",
“first_name” %><%= error_message_on “user”, “first_name” %>
Last Name: <%= text_field “user”,
“last_name” %>
Login Name:<font color=“green”,
size=“2”>(use A-Z, a-z, 0-9, “_”) <%= text_field “user”,
“login_name” %><%= error_message_on “user”, “login_name” %>
Password:<font color=“green”,
size=“2”>(password character range 5-40 )<%= password_field
“user”, “password” %>
Confirm Password: <%=
password_field “user”, “confirm_password” %>
* Email address: <%= text_field
“user”, “email” %>
<input type=“Submit”, value=“Sign up” /> <input type=“Reset”,
value=“Reset” />
<%= end_form_tag %>
|
Thnx again
Nitin
Hi Sijo,
I made changes as you said. Now to display errors used
<%= f.error_messages %>
I found it in the api.rubyrails.org website
(ActionView::Helpers::FormHelper)
but I am getting an error:
undefined method ‘error_messages’
Is there any other syntax to display these errors.
Thanks
Nitin
Hi Nitin M.
Can't ay without seeing code.Anyway try
<%= error_messages_for ‘user’ %> insted of f.error_messages
Sijo
Hi Nitin M.
It can be approached in a railsway like
First in views/users/new.html.erb add like below(I am writing only a
skeleton Please modify it).
Please fill the following details:
<% form_for(@user) do |f| %>
'*' fields are mandatory
* First Name: <%= f.text_field :first_name
%>
* Last Name: <%= f.text_field :last_name %>
-----
------etc
<%= f.submit 'SignUp' %>
<% end %>
And in controller what you have in 'signup' action move to 'create'
action
Sijo
Hi Nitin M.
Can you paste your relevant view and controller code?
Sijo
Sijo k g wrote:
Hi Nitin M.
Can't ay without seeing code.Anyway try
<%= error_messages_for ‘user’ %> insted of f.error_messages
Sijo
Hi Sijo,
That I have already tried. It did not work for me. [:(]
I don’t know where its going wrong. Anyways can you please tell
me if there is any other method to display error messages when
user does not enter data in the form as per mentioned in validation
in the model file.
Sorry for bothering you. But I need help.
Thanks
Nitin
Hey Sijo
I got other way to show messages. Thanks for your help.
Nitin