Remote form w/ evalScripts:true

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

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 :slight_smile: ). 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

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

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

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

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

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