Set session variable with link_to Rails

facebook, hyperlink, ruby-on-rails, session

Solution

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

Problem

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'});" } %> ```

Original source