Forum: Ruby on Rails rjs woes

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.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-14 20:01
(Received via mailing list)
i've just installed the RJS plugin per cody's instructiuons and read
through
tutorials.  I've made the necessary changes to my existing code and
everything appears to be working, except that the returned javascript is
not
being evaluated...

here's my setup:

i have a checkbox with an onclick event pointing to a javascript
function.
this javascript function does a few things then sends a request.  I have
the
onSuccess callback pointing to another function who's only purpose is to
eval(resp.responseText).  but this is not happening.  i added an alert(
resp.responseText) and the javascript code is there, it's just not being
evaluated.

checkbox:

<div id="cb_div">
<input type="checkbox" name="active_flag_<%= user.id -%>" value="<%=
user.id-%>" <%=
user.active? ? "checked" : "" -%> onclick="activate_user(this)"/>
</div>

javascript:

function activate_user(cb) {
  var status = (cb.checked) ? "1" : "0"
  var data = "id" + cb.value + "&af=" + status
  var myReq = new Ajax.Request( '/admin/users/activate', { :method:
'post',
parameters: data, onSuccess: success_handler, ...)
}

function success_handler(resp) {
  alert(resp.responseText);
  eval(resp.responseText);
}

rjs template:

page.visual_effect :highlight, "cb_div", { :start_color => "#aaffaa" }

the success_handler is getting called, as the alert shows the javascript
being returned as:

new Effect.Hilight('cb_div', {startcolor:#aaffaa});

but as i said, it's not getting evaluated in the eval() call. (i'm not
seeing the effect)

anyone have any ideas?

Chris
2ccb2f7699267d258faaafce4ee997df?d=identicon&s=25 Cody Fauser (Guest)
on 2006-03-15 07:06
(Received via mailing list)
Chris,

There are a few problems here.

> page.visual_effect :highlight, "cb_div", { :start_color => "#aaffaa" }

The option is actually :startcolor and it looks like you'll need extra
quotes on the hex value, like "'#aaffaa'" because the
scriptaculous_helper doesn't correctly place the :startcolor option
within quotes.

> the success_handler is getting called, as the alert shows the javascript
> being returned as:
>
> new Effect.Hilight('cb_div', {startcolor:#aaffaa});
Is this a typo or did you incorrectly spell Highlight here?

With the double quoting this will become:
new Effect.Highlight('cb_div', {startcolor:'#aaffaa'});

I tried this out in a quick test and without the extra quotes the
effect wasn't applied.  I got a nice green highlight with the extra
quotes.

> but as i said, it's not getting evaluated in the eval() call. (i'm not
> seeing the effect)

You shouldn't actually have to eval this yourself.  Prototype will
eval the response automatically because RJS sets the Content-Type
header to text/javascript.

Hope this helps.

>
--
Cody Fauser
http://www.codyfauser.com
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-15 14:45
(Received via mailing list)
Cody,

thanks so much for the assist.  the missing quotes was the issue.

and thanks for the plugin!

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