如何在Rails上实现无cookie的Flash消息? [英] How can I implement cookieless flash messages on Rails?

查看:64
本文介绍了如何在Rails上实现无cookie的Flash消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Facebook应用程序,因此由于P3P而不能依靠Cookie (隐私首选项项目),是的,这真是太痛苦了(请参阅解决方案

我终于找到了解决方法,它实现了我自己的(简单的)flash消息并将它们从一个请求传递到另一个请求./p>

首先,我覆盖了 application_controller.rb 中的 default_url_options ,以将:my_flash参数附加到每个请求:

  def default_url_options{:my_flash =>@my_flash}结尾 

然后,总是在 application_controller.rb 中,我在 my_flash_from_params 之前写了一个 my_flash_from_params 来设置 @my_flash 变量:

  def my_flash_from_params@my_flash =参数[:fb_flash]结尾 

最后,我在 application.html.erb

中渲染了以下 _my_flash.html.erb 部分

 < div class ="my_flash"><%= my_flash%></div> 

通话:

 <%= render:partial =>"layouts/my_flash",:locals =>{:my_flash =>@my_flash},如果@my_flash%> 

如果您想尝试此解决方案,另请参见有关default_url_options重写的答案.

I'm developing a facebook app so I can't rely on cookies due to P3P (Privacy Preferences Project) and yep, it's a damn pain (see slides 18 and 19 on this slideshare about Rails and Facebook apps for a picture of it)...

In a facebook app every cookie, from browsers perspective, is a third-party cookie. And many browsers block them by default.

So my question is: How can I implement flash messages without rely on cookies?

UPDATE:

I modified session_store.rb and the DB accordingly. Now the sessions are stored on DB but the flash messages are still relying on cookies... Any idea please?

UPDATE#2:

I finally found a workaround, see my answer below. Best thing to do would be to ajax everything (according to the above-linked slideshare) but as a quick fix my solution should work.

解决方案

I finally found a workaround implementing my own (simple) flash messages and passing them through the params from one request to another.

First of all, I overwritten default_url_options in application_controller.rb to append to every request a :my_flash param:

def default_url_options 
  { :my_flash => @my_flash }
end    

Then, always in application_controller.rb, I wrote a my_flash_from_params before_filter to set the @my_flash variable:

def my_flash_from_params
  @my_flash = params[:fb_flash]
end 

Finally I rendered the following _my_flash.html.erb partial in application.html.erb

<div class="my_flash">
  <%= my_flash %>
</div>  

Calling:

 <%= render :partial => "layouts/my_flash", :locals => {:my_flash => @my_flash} if @my_flash %>

If you want to try this solution see also this answer about default_url_options rewriting.

这篇关于如何在Rails上实现无cookie的Flash消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆