remote_function - how to include a id of current model

Hi all,

Hope someone can help me out with this weird problem, I'm sure it's
something simple.

All I want to do is send the ID of the current model from the view
into the controller via a remote_function call

basiclaly I'm adding a word to a list using a check box:

check_box_tag "list_ids[]", list.id, list.words.include?(word),
{ :onchange => remote_function(:url => { :controller =>
"list", :action => "add", :id => list.id }, :with => "word_id + '=' + #
{word.id}" ) }

The part which isn't working is specifically this:

:with => "word_id + '=' + #{word.id}"

If I remove it the code works but obviously I need that word.id in the
controller so I know which word to add to the list.

Thanks in advance!

Dave

Hi all,

Hope someone can help me out with this weird problem, I'm sure it's
something simple.

All I want to do is send the ID of the current model from the view
into the controller via a remote_function call

basiclaly I'm adding a word to a list using a check box:

check_box_tag "list_ids[]", list.id, list.words.include?(word),
{ :onchange => remote_function(:url => { :controller =>
"list", :action => "add", :id => list.id }, :with => "word_id + '=' + #
{word.id}" ) }

First off, you could just include word_id in the :url options. If you do want to use :with then the value needs to be a fragment of javascript that evaluates to an appropriate bit of query string - I've got some examples here: http://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterio

Fred

when you use remote_function the :with option actually expect
javascript code.
in your code remote_function helper is assuming "word_id + '=' + #
{word.id}" is
a javascript code.
so you can do any of the following two things
   :with=> "'word_id='+document.getElementById('word_id').value" where
word_id will be the id of that element
                               or
   you can send it to with the :url option

Thanks for the help, I didn't realise that you can simply add any
paramter to the url call, as so:

link_to_remote 'Click', :url => {:action => 'foo', :some_param =>
42, :something_else => 'hello world'}

the link to the blog post you posted was truncated and didn't work so
I had to google for it, so I've made a short link here:

http://bit.ly/link_to_remote

Cheers