error_messages_for Problem in ROR

I had a strange problem today where my field validation was working but error_messages_for giving away blank pages or nils.
So the solution of this problem is that we should use the variable as I have written below.


contact = Contact.new(params[:contact]) is not a correct format if
I want to retrieved my data
I should have retrieved it as Fllows:

@contact = Contact.new(params[:contact])

And most important thing is that never use redirect_to to retrieve data

you should use render.Basically redirect_to give you a new page rather than

the same page , but if you use render rather than redirect_to, then you will be on the same page on which you are having error that is why you

retrieve the data.

For example:

def new

@create=Create.new

end

def create

@create=Create.new(params[:create])

@create.username=params[:create][:username]

if request.post? && @create.save

redirect_to :action=>'profile',:controller=>'user'

else

render :action=>'new'

end


I think Now You can easily able to get the difference between them.

And for get error message as you have defined in model.

you should use the flash[:notice] like this.

flash[:notice] = @comment.errors.full_messages

render :action=>'new'


My Instagram