# Error handling on ajax form

**URL:** <https://www.ruby-forum.com/t/error-handling-on-ajax-form/177924>\
**Category:** Rails\
**Created:** [October 26, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/error-handling-on-ajax-form/177924 "2009-10-26T20:02:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [October 26, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/error-handling-on-ajax-form/177924/1 "2009-10-26T20:02:35Z")

</div>

I have a page displaying a form and a table. The form is used to add  
record to the table using ajax.  
I am using jQeury for this.  
Submitting the form with all fields filled in works just fine, but  
when I leave field empty it does not work.  
Probably because the jQuery code tries to add a partial (containing  
one row) to to the bottom of the table for the new object that could  
not be created because fields where empty.

My create action now looks like this:  
def create  
@project = Project.new(params[:project])  
@project.save  
@current\_project = @project  
flash[:notice] = ‘Successfully created project.’  
respond\_to do |format|  
format.js  
format.html { redirect\_to projects\_url }  
end  
end

What do I have to add to my code in order to get the form validation  
to work properly with my ajax form?

---

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [October 26, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/error-handling-on-ajax-form/177924/2 "2009-10-26T20:58:48Z")

</div>

On Oct 26, 2:12 pm, Ronald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a page displaying a form and a table. The form is used to add  
> record to the table using ajax.  
> I am using jQeury for this.  
> Submitting the form with all fields filled in works just fine, but  
> when I leave field empty it does not work.

What does not work? Is the Project not created? Are you getting an  
exception (and if so, what is the full exception + backtrace)?

> Probably because the jQuery code tries to add a partial (containing  
> one row) to to the bottom of the table for the new object that could  
> not be created because fields where empty.

Have you checked the development log to see if extraneous, blank  
fields actually are being sent in the POST request?

> end  
> You have no error checking in your controller, so if you’re wondering  
> why there’s no validation in your app, this is a good place to start.  
> Read  
> [http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html),  
> its an excellent Primer for how ActiveRecord validations work and how  
> to work with them in your controller code and views.

---

<div class="post-metadata">

**Author:** ![ronald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@ronald](https://www.ruby-forum.com/u/ronald)\
**Post date:** [October 26, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/error-handling-on-ajax-form/177924/3 "2009-10-26T23:05:17Z")

</div>

On 26 okt, 20:57, pharrington [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Oct 26, 2:12 pm, Ronald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a page displaying a form and a table. The form is used to add  
> > record to the table using ajax.  
> > I am using jQeury for this.  
> > Submitting the form with all fields filled in works just fine, but  
> > when I leave field empty it does not work.
> 
> What does not work? Is the Project not created? Are you getting an  
> exception (and if so, what is the full exception + backtrace)?

When I submit the empty form nothing happens in the browser. The  
project is not created also no exception is raised.  
On the project model I defined these validations:  
validates\_presence\_of :name  
validates\_presence\_of :description

> > Probably because the jQuery code tries to add a partial (containing  
> > one row) to to the bottom of the table for the new object that could  
> > not be created because fields where empty.
> 
> Have you checked the development log to see if extraneous, blank  
> fields actually are being sent in the POST request?  
> Yes, blank fields are send in the POST request.

In the development log i found this:  
ActionView::TemplateError (edit\_project\_url failed to generate from  
{:controller=\>“projects”, :action=\>“edit”, :id=\>#\<Project id: nil,  
name: “”, description: “”, created\_at: nil, updated\_at: nil,  
startdate: “2009-10-26”, enddate: “2009-10-26”\>}, expected:  
{:controller=\>“projects”, :action=\>“edit”}, diff: {:id=\>#\<Project id:  
nil, name: “”, description: “”, created\_at: nil, updated\_at: nil,  
startdate: “2009-10-26”, enddate: “2009-10-26”\>}) on line #12 of app/  
views/projects/\_project.html.erb:  
9: \<%=h project.enddate %\>  
10:   
11: \<%= link\_to “Show”, project %\>  
12: \<%= link\_to “Edit”, edit\_project\_path(project) %\>  
13: \<%= link\_to “Destroy”, project, :confirm =\> ‘Are you  
sure?’, :method =\> :delete %\>  
14:   
15:

This is there because my javascript code in create.js.erb that gets  
executed on the format.js response tries to render that \_project  
partial in my view.

> > ```
> > end
> > 
> > ```
> > 
> > end
> 
> You have no error checking in your controller, so if you’re wondering  
> why there’s no validation in your app, this is a good place to start.  
> Readhttp://guides.rubyonrails.org/activerecord\_validations\_callbacks.html,  
> its an excellent Primer for how ActiveRecord validations work and how  
> to work with them in your controller code and views.

Thanks for the link.

Of course I can change the create action something like this:  
def create  
@project = Project.new(params[:project])  
if @project.save  
@current\_project = @project  
flash[:notice] = ‘Successfully created project.’  
respond\_to do |format|  
format.js  
format.html { redirect\_to projects\_url }  
end  
else  
flash[:notice] = ‘Failed to create a new project.’  
render :action =\> ‘index’  
end  
end

But how do I show the validation messages on the index view?  
In my view I have this:  
\<%= error\_messages\_for :project %\>  
But it does not show anything when I submit an empty form with the  
create action code from just above.  
Also the ‘Failed to create a new project.’ does not show.
