本文介绍了使用HTTP PUT时,Rails的会话是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,其中一个特别的环节是造成空会话哈希。这是不好的,因为我需要通过SESSION_ID找到一个模型。

I have a situation, where one particular link is resulting in an empty session hash.This is not good as I need to find a model by using the session_id.

这是造成故障的链接是:

The link that is causing trouble is:

<div id="marker_images">
  <% @marker_image_urls.each do |image_url| %>
    <%= link_to( image_url,
                 location_type_path(@location_type.id,
                 :location_type => {:preset_marker_url => image_url}),
                 :method => :put,
                 :remote => true ) %>
  <% end %>
</div>

和code,发现从会话ID的模式(这是使用的before_filter叫):

and the code that finds the model from the session id (which is called using a before_filter):

def get_organisation
  @organisation = Organisation.find_by_session_id(session[:session_id])
end

在调试模式下,会话 == {}

In debugger mode, session == {}

如果我改变的link_to是一个HTTPGET,而不是放,该会议被发送。然而,这种要求是不适合于一个获取,因为它正在修改的内容

If I change the link_to to be a HTTP 'get' instead of 'put', the session is sent.However, this request isn't appropriate for a 'get' as it is modifying data.

为什么会'得到'包括会话,而是把'不?

Why would 'get' include the session, but 'put' not?

推荐答案

好了,找到了。由于该链接是一个HTTP-放,导轨不会自动包含的真实性标记,因为它与一个基于HTTP-GET。因此,通过传递的真实性令牌作为参数,轨道识别会话。

Ok, found it.Because the link is a http-put, rails does not automatically include the authenticity token, as it does with an http-get. So, by passing the authenticity token as a param, rails recognises the session.

<div id="marker_images">
  <% @marker_image_urls.each do |image_url| %>
    <%= link_to( image_tag(image_url),
                 location_type_path(@location_type.id,
                                    :location_type => {:preset_marker_url => image_url},
                                    :authenticity_token => form_authenticity_token),
                 :method => :put,
                 :remote => true ) %>
  <% end %>
</div>

本页面帮我出在这个解决方案结结巴巴:http://www.kolodvor.net/2010/01/02/rails-csrf-and-ajax-requests/

This page helped me out in stumbling upon this solution: http://www.kolodvor.net/2010/01/02/rails-csrf-and-ajax-requests/

这篇关于使用HTTP PUT时,Rails的会话是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:40