4

Is it possible to set a session variable with a link_to? I don't want to set a parameter, because I have a couple redirects and it gets wiped away.

i.e. I want to set a session variable "modelid" to "you" with a link.

I want to set a session variable while the FB login oauth runs...

<%= link_to "New Post", "#", {:class => "btn btn-primary btn-large inline pull-left", :onclick => "FB.login(function(response){},{perms:'email, publish_stream, user_photos'});" } %>
user749798
  • 5,210
  • 10
  • 51
  • 85

1 Answers1

7

You can create an action in one of your controllers that sets this session variable and then call it with an AJAX request.

routes:

post "/myroute", :to=>"some_controller#set_my_session_var"

some_view.html.erb (jquery example):

$(function(){
  $("#my-button").click(function(){
    $.post('/myroute');
  })
})

some_controller.rb:

def set_my_session_var
  session[:somekey]='somevalue'
end
miked
  • 4,489
  • 1
  • 17
  • 18
  • I am setting session variable in a controller's method and trying to access same variable in another controller. But its showing me value nil!!! I have checked that value was setup in the variable while setting up – RAJ Nov 26 '12 at 18:33
  • What exactly are you sending to the /myroute action? Or in other words how to send the data to that action – gates Apr 19 '16 at 07:13
  • I have the same problem, setting a value is not present anymore after redirecting on the method that set the session value in. – alexventuraio Dec 20 '20 at 21:48