Forum: Ruby on Rails change a value inside button_to_function with js

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
LuCa (Guest)
on 2007-04-09 20:57
Hi All

I'm using the following in my .rhtml

  <%= button_to_function 'setup', remote_function( :url => { :action =>
'abc', :controller => 'def', :xyz => '10'}) %>"

However, depending on what the user does, I would like to change the
value for 'xyz' using javascript (the server will do stuff depending on
this value)
So, this value is known inside the javascript object I'm using, but how
do I get it dynamically inside the button_to_function ?

Thnx
LuCa
LuCa (Guest)
on 2007-04-09 22:57
Maybe I should ask the question differently!

Can I include javascript stuff inside the button_to_function helper ?
or
can I use something like <%= button_to_function .... %> inside
javascript (.js file) ?

I know one way to solve this, which is to look at the source of the
created html, copy everything thats created by the 'button_to_function'
helper and put this in my .js files and use the innerHTML to replace the
button each time the values change. But that doesn't sound like the
correct way of working with RoR.

Any suggestions ?

Thnx
LuCa
Brian C. (Guest)
on 2007-04-10 02:10
give the button an id, then you should be able to access the element and
change any value through the DOM. the button_to helper will allow you to
do this, but im not sure if button_to_function will - you may have do
some html yourself.

eg
<input id="updatable_button" value="Im a Button!" type="button"/>

<script>
<!-- do somthing like this when a user changes a value -->
$('updateable_button').value = "Im a changed Button!"
</script>

LuCa wrote:
> Maybe I should ask the question differently!
>
> Can I include javascript stuff inside the button_to_function helper ?
> or
> can I use something like <%= button_to_function .... %> inside
> javascript (.js file) ?
>
> I know one way to solve this, which is to look at the source of the
> created html, copy everything thats created by the 'button_to_function'
> helper and put this in my .js files and use the innerHTML to replace the
> button each time the values change. But that doesn't sound like the
> correct way of working with RoR.
>
> Any suggestions ?
>
> Thnx
> LuCa
Steve M. (Guest)
on 2007-04-10 02:53
(Received via mailing list)
Hi,

I've been toying with something similar which might help you. In
remote_function method, I wanted to insert some javascript to
dynamically calculate the URL but the remote_function method wanted to
use a string for URL. So I kludged my way through it with the
following:

remote_function(:url => "'+this.form.action+'", :update => "'+this.id
+'_update", ...[other params]...)

Can you see what I'm doing? It's hard to read - note that everything
between the double quotes is all javascript. It's easier to read once
you know that. Basically, I'm writing javascript that closes the
string quote that remote_function opens for :url, and then I insert
some javascript code (and reopen the string and add text in the case
of the "update" param). This results in Javascript that looks like:

new Ajax.Updater(''+this.id+'_update', ''+this.form.action+'')

I think it's a pretty clean way of generating dynamic JS code in Rails
js helper functions. It's a little fugly but not too bad..

hth,

Steve



On Apr 9, 3:10 pm, Brian C. <removed_email_address@domain.invalid>
LuCa (Guest)
on 2007-04-11 21:04
so finally time to try your suggestions, what I see is (in the generated
HTML code):

<input id="conf_button" onclick="new
Ajax.Request('/my_controlller/my_action?game=%27%2Bmy_js_object......

So you see that I get thing like %27%sB for the '+

All this is generated in the .rhtml file, maybe you are doing this in a
.js file ?

Hopy you have an idea of what I do wrong!

Thnx
LuCa
Steve M. (Guest)
on 2007-04-23 21:48
(Received via mailing list)
Hi,

I don't think you're doing anything wrong. The conversion to %27 is
normal behavior - the javascript is being "escaped" by Ruby. That's
the benefit of calling the ruby functions for javascript. You could do
the same thing with .rjs files - I prefer to write everything in
"normal" Rails.

Let me know if you're still having trouble (you can reply to me
directly).

Steve
This topic is locked and can not be replied to.