You seem to be confusing:
a) JavaScript with Ruby, and
b) server-side scripting with client-side scripting.
What you have happening right now is:
a) Your web browser JavaScript code is called invoking the $.post()
function.
This sends an HTTP request to the server.
b) Your Ramaze 'rate' action is called.
This happens to set a @rating instance variable in the Ruby
environment on the server.
Then is creates an HTTP response with some contents (possibly
@rating.to_s) and sends it back to your web browser.
c) Your web browser gets the response and, through the miracle of
jQuery, invokes your anonymous callback function.
Therein you construct a string called result that happens to look like
it has Ruby interpolation in it.
Your web browser has no way to access instance variables from the
server, possibly thousands of miles away.
Perhaps try something like:
$.post( "/rate", { rating: $(this).html()}, function( resultObject,
status ){jax
// Assumes you're using Safari, Chrome, or Firefox with Firebug
installed
console.dir( resultObject );
}, 'json' );
...
def rate
if request.xhr? # came from ajax request
"{ rating: #{request[:rating].to_i} }"
else
request[:rating].to_s
end
end
> And here's the rate() method from my controller:
>
> <<
> def rate
> Ramaze::Log.debug "Enter rate"
> if request.post?
> Ramaze::Log.debug "Rating selecte = #{request[:rating]}"
> @rating = request[:rating]
Is this actually a symbol, or a string (i.e. request['rating']) ?
--
James Britt
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
www.neurogami.com - Smart application development
Let's try that again without my 8-month old climbing all over the
keyboard:
$.post( "/rate", {rating:$(this).html()},
function( resultObject, status ){
//resultObject will be a JS object (aka Hash)
if (status=="success"){
// Safari, Chrome, or Firefox with Firebug installed
Per the documentation for the jQuery $.post() function[1], when the
fourth parameter is the string "json", the data returned from the web
server as the response—which, aside from http headers, is just a big
blob of bytes that might be html, javascript, a binary graphic, etc.—
is interpreted as a JSON data object. This sets the first parameter
passed to your function callback as a JavaScript object, which is
similar to a Ruby Hash. (In a JS Object, all keys are strings.
myObject.foo is the same as myObject["foo"], and myObject[1] is the
same as myObject["1"].)
Dumping content to the FireBug console does not interrupt the
JavaScript or HTML processing. You don't need to resume from it.
console.debug( "foo" ) is like Ramaze::Log.debug("foo"), except of
course that it comes out in a different spot. console.dir() is a
convenience function that, given a JS object, provides an interactive
hierarchical representation of it.
If that is dumping out a big string that looks like the page, my guess
is that the output string of your "rate" action is getting wrapped in
a Ramaze layout. Does that seem likely?
The simplest possible example is not to supply "json" as the fourth
parameter to $.post(), in which case the first parameter to your
callback function should just be the text sent by your action.
Something like:
$.post( "/rate", { foo: "bar" }, function( response ){
console.log( "Rate sent back: " + response );
} );
-_-_-_-_-_-_-
def rate
# Make sure this action doesn't use a layout
"OH HAI! You sent me: #{request['foo'}"
end
Thanks to Max A for pointing out that my test for the success status
of the AJAX is apparently superfluous when using $.post()
[1] http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype
I believe so. See manveru's first response to my long initial post for
various ways that you can control whether or not an action (or a
particular request for an action) will use a layout:
> I'll give that a shot. Once again, thanks for all your help.
FWIW, I added a Layout Helper to Ramaze, and my patch was accepted
into the master branch. I made it because of there have been several
people in IRC wondering just what happened to 2009.03 style layouts.
:) The new Layout Helper is a convenience wrapper for the more
powerful, but perhaps more low-level 2009.05 layout method.
I think manveru is aiming to get a new Ramaze release out this coming
week, but you can look over the Layout Helper now, in case you are
willing to try the git version of Ramaze:
I also added specs that demonstrate usage even more.
--
Pistos
http://blog.purepistos.net
Time#to_s is not valid JSON, and JSON has no type that represents Time
directly. You will have to convert it to a Date object in the JS client.
Doing that is easiest if you go via a unix timestamp (Time#to_i). In Ruby you
can use the Time::at method to create a Time instance from a unix timestamp
although you'll loose all sub-second precision.
require 'json'; require 'time'
# true
time = Time.now
# 2009-06-01 12:58:59 +0900
time.to_s
# "2009-06-01 12:58:59 +0900"
JSON.parse %[ { "time": #{time} } ]
JSON::ParserError: 598: unexpected token at '{ "time": 2009-06-01 12:58:59 +0900 } '
from /home/manveru/.gem/ruby/1.9.1/gems/json-1.1.3/lib/json/common.rb:122:in `parse'
from /home/manveru/.gem/ruby/1.9.1/gems/json-1.1.3/lib/json/common.rb:122:in `parse'
from (irb):6
from /usr/bin/irb:12:in `<main>'
JSON.parse %[ { "time": #{time.to_i} } ]
# {"time"=>1243828739}
puts({:time => time}.to_json)
{"time":"2009-06-01 12:58:59 +0900"}
# and here we play a bit with (de)serialization
time1, time2 = Time.now, Time.now
# [2009-06-01 13:07:15 +0900, 2009-06-01 13:07:15 +0900]
time1 == time2
# false
Time.at(time1.to_i) == Time.at(time2.to_i)
# true
time1.to_i
# 1243829235
time2.to_i
# 1243829235
>
> Scott
>
> On Sun, May 31, 2009 at 5:39 PM,
> <jesusisramaz...@geoshell.com>wrote:
>
> >
> > 2009/5/30 Scott LaBounty - slab...@gmail.com:
> > >> > `deny_layout' for MainController:Class (NoMethodError)
> > >> >
> > >> > Did this get removed in the new Ramaze?
> > >>
> > >> I believe so. See manveru's first response to my long initial
> > >> post for various ways that you can control whether or not an
> > >> action (or a particular request for an action) will use a layout:
> >
> > > I'll give that a shot. Once again, thanks for all your help.
> >
> > FWIW, I added a Layout Helper to Ramaze, and my patch was accepted
> > into the master branch. I made it because of there have been
> > several people in IRC wondering just what happened to 2009.03 style
> > layouts. :) The new Layout Helper is a convenience wrapper for the
> > more powerful, but perhaps more low-level 2009.05 layout method.
> >
> > I think manveru is aiming to get a new Ramaze release out this
> > coming week, but you can look over the Layout Helper now, in case
> > you are willing to try the git version of Ramaze:
> >
> >
> > http://github.com/manveru/ramaze/blob/a6345820c5ac949a95eb3385881c9cba995b6a77/lib/ramaze/helper/layout.rb
> >
> > I also added specs that demonstrate usage even more.
> >
> >
> > --
> > Pistos
> > http://blog.purepistos.net
> >
> > >
> >
>
>
--
^ manveru