# Model validation

**URL:** <https://www.ruby-forum.com/t/model-validation/174274>\
**Category:** Rails\
**Created:** [August 24, 2009, 8:46am UTC](https://www.ruby-forum.com/t/model-validation/174274 "2009-08-24T08:46:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 8:46am UTC](https://www.ruby-forum.com/t/model-validation/174274/1 "2009-08-24T08:46:16Z")

</div>

hello guys

i am using model validation with ajax

i have new.html.erb

it is call through link\_\_to\_remote()

then press create if any validate require page will redirect to it  
actual controller for ex. admins/new something like this

i want display validation message on same page in this command url can  
not change in all page url remain same, if any one having idea about it  
please share with me.

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/model-validation/174274/2 "2009-08-24T12:04:31Z")

</div>

## index.html.erb

 \<%= link\_to\_remote('New', :update =\> 'pravin',:url =\> {:action =\> :new})%\>
---------------------- new.html.erb ----------------------

# New animal
\<% form\_for @animal do |f|%\> 

Name

 \<%= f.text\_field :name%\> 

Desc

 \<%= f.text\_field :desc %\> 

\<%= submit\_tag 'Create' %\>

\<% end %\> \<%= image\_tag 'rails.PNG', 'rails.png', :size =\> '70x70'%\> \<%= link\_to 'Back', animals\_path %\>
---------------------- it is my contrller -------------------- def create @animal = Animal.new(params[:animal]) respond\_to do |format| if @animal.save flash[:notice] = 'Animal was successfully created.' format.html { redirect\_to(@animal) } format.xml { render :xml =\> @animal, :status =\> :created, :location =\> @animal } else format.html { render :action =\> "new" } format.xml { render :xml =\> @animal.errors, :status =\> :unprocessable\_entity } end end end ---------------- model vaidation ---------------- class Animal \< ActiveRecord::Base validates\_presence\_of :name,:desc end ------------------------ i want to show validation on same page whenever refresh page go to on main page

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 11:48am UTC](https://www.ruby-forum.com/t/model-validation/174274/3 "2009-08-24T11:48:13Z")

</div>

Wap A. wrote:

> hello guys
> 
> i am using model validation with ajax
> 
> i have new.html.erb
> 
> it is call through link\_\_to\_remote()
> 
> then press create if any validate require page will redirect to it  
> actual controller for ex. admins/new something like this
> 
> i want display validation message on same page in this command url can  
> not change in all page url remain same, if any one having idea about it  
> please share with me.

Can you show me your code.

method in controller + html view all code.

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 12:16pm UTC](https://www.ruby-forum.com/t/model-validation/174274/4 "2009-08-24T12:16:57Z")

</div>

def create  
@animal = Animal.new(params[:animal])  
respond\_to do |format|  
#INCLUDE THIS LINE

```
 if @animal.save
   flash[:notice] = 'Animal was successfully created.'
   format.html { redirect_to(@animal) }
   format.xml { render :xml => @animal, :status => :created,

```

:location =\> @animal }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @animal.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

On Mon, Aug 24, 2009 at 3:34 PM, Wap A.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/model-validation/174274/5 "2009-08-24T12:25:51Z")

</div>

mahesh s wrote:

> Maheshror
> 
> def create
> 
> > @animal = Animal.new(params[:animal])  
> > respond\_to do |format|  
> > #INCLUDE THIS LINE  
> > if @animal.valid?  
> > #if @animal.save
> 
> animal.save
> 
> > end
> 
> try this  
> ok dear but it can not save record on table this is new problem.  
> i think u tell me like this ?

* * *

## def create @animal = Animal.new(params[:animal]) respond\_to do |format| #if @animal.save if @animal.valid? flash[:notice] = ‘Animal was successfully created.’ format.html { redirect\_to(@animal) } format.xml { render :xml =\> @animal, :status =\> :created, :location =\> @animal } else format.html { render :action =\> “new” } format.xml { render :xml =\> @animal.errors, :status =\> :unprocessable\_entity } end end end

---

<div class="post-metadata">

**Author:** ![wap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d9b06d/32.png) [@wap](https://www.ruby-forum.com/u/wap)\
**Post date:** [August 24, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/model-validation/174274/6 "2009-08-24T12:18:42Z")

</div>

Maheshror

def create

> @animal = Animal.new(params[:animal])  
> respond\_to do |format|  
> #INCLUDE THIS LINE  
> if @animal.valid?  
> #if @animal.save

animal.save

> end

try this
