Forum: Ruby on Rails setting session variable with a checkbox

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.
Mike D. (Guest)
on 2007-07-20 23:09
Hi:

Is there a way to set a session variable in a check box that's not in a
form nor linked to a controller?  The checkbox value would also be the
value of the session  variable I want it to set.  I assume I have to put
this in a form.  Any suggestions?

Thanks very much in advance!

Mike
Shai R. (Guest)
on 2007-07-22 12:14
Mike D. wrote:
> Hi:
>
> Is there a way to set a session variable in a check box that's not in a
> form nor linked to a controller?  The checkbox value would also be the
> value of the session  variable I want it to set.  I assume I have to put
> this in a form.  Any suggestions?
>
> Thanks very much in advance!
>
> Mike

just an idea, but you could do an ajax call on an onchange event of the
checking-of-the-box:

<input type="checkbox" value="parameter_for_session"
name="session_setter" onchange="ajax_call('<%= url_for
:controller=>:example, :action=>:set %>')">

create the function
<script type="text/javascript">

function ajax_call(url) { new Ajax.Request(url, {asynchronous:true,
evalScripts:true, parameters:Form.serialize(this)}); return false; }

</script>


and then just do an action in the ExampleController

def set
  session[:checkbox] = params[:session_setter]
  render :nothing => true
end

all untested code...might need to modify the ajax statement above to
pass the param[:session_setter] in "parameters:..." but an idea, no?
hth
This topic is locked and can not be replied to.