# Forms, view & AJAX best practice help

**URL:** <https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541>\
**Category:** Rails\
**Created:** [April 4, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541 "2006-04-04T16:40:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 4, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541/1 "2006-04-04T16:40:16Z")

</div>

What i’m trying to get working is I have a controller called  
“permissions” and a view “permissions.rhtml”. This view has a hidden div  
that contains a form for adding new roles. Its just a single textfield.  
Also on the page I have a dropdown list of roles and then underneath  
that a div that contains a list of permissions for the role selected in  
the dropdown. I have a link that when clicked makes the hidden form div  
visible.

What I would like is for the form to submit via AJAX to a new\_role  
controller method. Any errors would be highlighted in my  
“error\_messages\_for ‘role’” call at the top of the permissions.rhtml  
file. When it has submitted correctly the dropdown list would be updated  
with the new selection.

What I have got so far is that the page wants to show a “new\_form” page?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 4, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541/2 "2006-04-04T17:29:02Z")

</div>

James W. wrote:

> What i’m trying to get working is I have a controller called  
> “permissions” and a view “permissions.rhtml”. This view has a hidden div  
> that contains a form for adding new roles. Its just a single textfield.  
> Also on the page I have a dropdown list of roles and then underneath  
> that a div that contains a list of permissions for the role selected in  
> the dropdown. I have a link that when clicked makes the hidden form div  
> visible.
> 
> What I would like is for the form to submit via AJAX to a new\_role  
> controller method. Any errors would be highlighted in my  
> “error\_messages\_for ‘role’” call at the top of the permissions.rhtml  
> file. When it has submitted correctly the dropdown list would be updated  
> with the new selection.
> 
> What I have got so far is that the page wants to show a “new\_form” page?

Ok I will rephrase it:

How can I get Error messages to show when using ajax form submit?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 4, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541/3 "2006-04-04T18:28:11Z")

</div>

James  
\> How can I get Error messages to show when using ajax form submit?

In the controller’s response to the submit, you will send an html  
fragment with RJS code like:  
…  
page.replace\_html “the\_div\_id”, :partial =\> ‘some\_html\_fragment’  
…

The trick is for the html fragment to include the error displaying Rails  
code :  
…  
\<%= error\_messages\_for ‘foo’ %\>  
…

Alain

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [December 25, 2006, 2:53am UTC](https://www.ruby-forum.com/t/forms-view-ajax-best-practice-help/93541/4 "2006-12-25T02:53:52Z")

</div>

or you could do something like this without having to use a partial

```
     render :update do |page|
        page.replace_html 'address_errors', :inline=>"<%= 

```

error\_messages\_for :address %\>"  
end
