Forum: Ruby on Rails Returning values from javascript within a controller

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.
8a3abcfeae74fa0ea68bb23ef0501511?d=identicon&s=25 Nathaniel (Guest)
on 2007-03-08 12:18
Is there a way that I can get hold of the result of a javascript
function call from within a controller?

There are a few instances where I want to do this. For example, suppose
I would like to know if the page to be rendered is within a frame.

Within javascript I can determine if the page is within a frame by
evaluating either of the conditions:

  parent.frames.length != 0

  or

  parent.location && (parent.location != window.location)

I don't know how to do this within Rails, and it would be useful to be
able to render different content depending on this state.

Besides, for reasons too messy to explain, I use javascript to maintain
a much more complicated "state" structure (that reacts to events and
such) that is way too transitory to store within the database.

I would love to be able to query this state from within my controller.

Thanks, Nat
8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-03-08 15:29
(Received via mailing list)
Rails can only get a hold of information that you send to it, so
you'll need to encode this state information on the query string and
have your rails controllers handle it along with the rest of the
request params.

In the case if your frames example, you'll need to attach some sort of
information on the query string to let it know that you're loading the
action up in a frame.

e.g.:
<script>
  frames[0].location = "/controller/action?frame=true";
  // or something similar

  var pairs = [];
  for (var p in state) pairs.push(escape(p + "=" + state[p]);
  frames[1].location = "/controller/action?state=" + pairs.join(',');
</script>

in your controller, get the state by decoding the junk you encoded:
  state = params[:state].split(',').map { |s| s.split('=') }.inject({})
{|h,k| h[k.first] = k.last; h}
0c96554da21936addce011427bd0554d?d=identicon&s=25 vin (Guest)
on 2007-03-11 02:26
Nathaniel wrote:
> Is there a way that I can get hold of the result of a javascript
> function call from within a controller?
>
> There are a few instances where I want to do this. For example, suppose
> I would like to know if the page to be rendered is within a frame.
>
> Within javascript I can determine if the page is within a frame by
> evaluating either of the conditions:
>
>   parent.frames.length != 0
>
>   or
>
>   parent.location && (parent.location != window.location)
>
> I don't know how to do this within Rails, and it would be useful to be
> able to render different content depending on this state.
>
> Besides, for reasons too messy to explain, I use javascript to maintain
> a much more complicated "state" structure (that reacts to events and
> such) that is way too transitory to store within the database.
>
> I would love to be able to query this state from within my controller.
>
> Thanks, Nat

Here is suggestion.

Create a hidden field on the form and populate the field from java
script result.

The field will be posted back to the controller as a parameter.

Hope this is of help.

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