【问题标题】:Can't get Sinatra Flash Gem to work无法让 Sinatra Flash Gem 工作
【发布时间】:2013-02-08 05:29:08
【问题描述】:

我正在尝试在成功提交表单后显示快速通知。在 Rails 中,我会在控制器中使用类似的东西:

:notice => "Youve submitted the form"

我遇到了Sinatra flash gem,并希望在重定向后显示一条 Flash 消息。我像这样安装了 gem 和设置:

myapp.rb:

require 'sinatra/flash'
enable :sessions

#form config
 )}
redirect '/success' # this is the hook after my form submission
end

get('/success') do
 flash[:success] = "Thanks for your email. I'll be in touch soon."
 erb :index 
end

所有发生的事情是我被重定向到没有 Flash 消息的索引页面。查看文档,这就是我需要做的所有事情。有没有人觉得有什么不同?

【问题讨论】:

  • 我不确定,但您是否在 index.* 视图中添加了适当的代码?像 = flash
  • 你能不能稍微扩展一下,不太清楚你的意思

标签: ruby sinatra alert


【解决方案1】:

我将我的 Flash 通知移到重定向之前,以便它可以保存消息:

flash[:notice] = "Thanks for your email. I'll be in touch soon."
redirect '/success'

get('/success') do
 erb :index 
end

然后,在我看来,我现在把它放在顶部:

<div id='flash' class='notice'>
 <a class="close" data-dismiss="alert">&#215;</a>
 <%= flash[:notice] %>
</div>

它需要一些样式,但可以。如果有人有更好的解决方案,请分享。

【讨论】:

  • 你也可以在你的视图中使用styled_flash helper来显示flash消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多