Set session variable with link_to Rails

泪湿孤枕 提交于 2019-12-10 16:22:37

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/12043737/set-session-variable-with-link-to-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!