# Remote form w/ evalScripts:true

**URL:** <https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872>\
**Category:** Rails\
**Created:** [July 3, 2006, 12:18am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872 "2006-07-03T00:18:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 12:18am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/1 "2006-07-03T00:18:17Z")

</div>

Hello. I want to select an item from a select list and have that  
result in an ajax call that updates two sections on the page. Here is  
the code in the controller

def create  
…  
render :update do |page|  
page.replace\_html ‘list\_items’, :partial =\> ‘lists/list\_items’  
page.replace\_html ‘add\_item’, :partial =\> ‘lists/add\_item’  
end  
end

Here is the version of code in the template that I WANT to use to  
POST to this action:

\<%= form\_remote\_tag :html =\> { :action =\> url\_for(:controller =\>  
“list\_item\_links”, :action =\> “create”) }, :eval\_scripts =\> true %\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_id’, :value =\> @list.id%\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_item\_type’, :value =\> ‘List’ %\>  
Which list?  
\<%= select ‘list\_item\_link’, ‘list\_item\_id’, @lists.collect{ |l|  
[l.name, l.id] },  
{ :include\_blank =\> true},  
{ :class =\> “formfield”, :onchange =\> “this.form.submit()” }  
%\>  
\<%= end\_form\_tag %\>

And here is the code in the template that actually works (using a GET  
instead):

Which list?  
\<%= select ‘list\_item\_link’, ‘list\_item\_id’, @lists.collect{ |l|  
[l.name, l.id] },  
{ :include\_blank =\> true},  
{ :class =\> “formfield”, :onchange =\> “new  
Ajax.Request(’/admin/list\_item\_links/create?list\_item\_link  
[list\_item\_type]=List&list\_item\_link[list\_id]=#{@list.id}  
&list\_item\_link[list\_item\_id]=’ +  
this.options[selectedIndex].value,{asynchronous:true,  
evalScripts:true}); return false;” }  
%\>

The first example, using the form, results in javascript being  
rendered in the browser rather than being executed. Note that it does  
say “:eval\_scripts =\> true” and that the resulting javascript does  
include “evalScripts:true”.

I’m using rails 1.1.2.

Thanks in advance,  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 12:56am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/2 "2006-07-03T00:56:46Z")

</div>

It occurs to me that this example would be easier to grok - the other  
example is adding a list to another list (we’ve got lists of  
anything, including other lists 🙂 ). Here’s the same example only  
the goal is to add a story to a list:

DESIRED (but doesn’t work)  
\<%= form\_remote\_tag :html =\> { :action =\> url\_for(:controller =\>  
“list\_item\_links”, :action =\> “create”) }, :eval\_scripts =\> true%\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_id’, :value =\> @list.id%\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_item\_type’, :value =\>  
‘Story’ %\>  
Which story?  
\<%= select ‘list\_item\_link’, ‘list\_item\_id’, @stories.collect{ |l|  
[l.name, l.id] },  
{ :include\_blank =\> true},  
{ :class =\> “formfield”, :onchange =\> “this.form.submit()” }  
%\>  
\<%= end\_form\_tag %\>

WORKING (but not desired)  
Which story?  
\<%= select ‘list\_item\_link’, ‘list\_item\_id’, @stories.collect{ |l|  
[l.name, l.id] },  
{ :include\_blank =\> true},  
{ :class =\> “formfield”, :onchange =\> “new  
Ajax.Request(’/admin/list\_item\_links/create?list\_item\_link  
[list\_item\_type]=Story&list\_item\_link[list\_id]=#{@list.id}  
&list\_item\_link[list\_item\_id]=’ +  
this.options[selectedIndex].value,{asynchronous:true,  
evalScripts:true}); return false;” }  
%\>

Thanks again,  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 1:00am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/3 "2006-07-03T01:00:41Z")

</div>

On Jul 2, 2006, at 3:55 PM, David C. wrote:

> ‘Story’ %\>  
> \<%= select ‘list\_item\_link’, ‘list\_item\_id’, @stories.collect{ |l|  
> Thanks again,  
> David

```
How are you handling the controller side of the desired version? Are

```

you using rjs? If you are using rjs then you don’t need eval\_scripts.

-Ezra

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 6:01am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/4 "2006-07-03T06:01:07Z")

</div>

Ezra,

Thanks for your reply.

I’ve tried this with and without :eval\_scripts, using an rjs file and  
also embedding the rjs in the controller method:

def create  
…  
render :update do |page|  
page.replace\_html ‘list\_items’, :partial =\> ‘lists/list\_items’  
page.replace\_html ‘add\_item’, :partial =\> ‘lists/add\_item’  
end  
end

No matter which way I do it, it seems that the form tag approach  
results in javascript getting rendered in the browser, but the select  
list calling the remote function directly results in the same  
javascript being executed.

Any other ideas? Can anyone post a working example of doing what I’m  
trying to do, which is to submit a form on selecting from a select  
list and then updating two sections on the page?

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 7:02am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/5 "2006-07-03T07:02:09Z")

</div>

Did you try with

this.form.onsubmit()

Stephan

David C. wrote:

> Well, I figured out the root of the problem. The resulting html is this:
> 
> ... ...
> 
> From what I can tell, when you submit the form using  
> onchange=“this.form.submit()” it bypasses the onsubmit event in the  
> form tag. That make sense? So I have to play around w/ some other  
> approach, like a remote\_function that has access to the form. I’ll  
> keep trying - but please let me know if anyone has a solution.
> 
> Thanks,  
> David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 6:34am UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/6 "2006-07-03T06:34:31Z")

</div>

Well, I figured out the root of the problem. The resulting html is this:

... ...

From what I can tell, when you submit the form using  
onchange=“this.form.submit()” it bypasses the onsubmit event in the  
form tag. That make sense? So I have to play around w/ some other  
approach, like a remote\_function that has access to the form. I’ll  
keep trying - but please let me know if anyone has a solution.

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [July 3, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/remote-form-w-evalscripts-true/96872/7 "2006-07-03T12:55:51Z")

</div>

For anyone interested, here is a solution that seems to work:

\<%= form\_tag %\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_id’, :value =\> @list.id%\>  
\<%= hidden\_field ‘list\_item\_link’, ‘list\_item\_type’, :value =\>  
‘Story’ %\>  
Which story?  
\<%= select ‘list\_item\_link’, ‘list\_item\_id’, @stories.collect{ |list|  
[list.name, list.id] },  
{ :include\_blank =\> true},  
{ :class =\> “formfield”,  
:onchange =\> remote\_function(  
:url =\> url\_for(:controller =\> ‘list\_item\_links’, :action =\>  
‘create’),  
:with =\> ‘Form.serialize(this.form)’  
)  
}  
%\>  
\<%= end\_form\_tag %\>

which produces this:

Which story? Biography Turning the Tide
