Rails flash.now 具有任意密钥的哈希值?

发布于 2024-12-08 06:53:51 字数 1096 浏览 0 评论 0原文

所以我正在做一些基本的验证并尝试向闪存哈希添加任意密钥(你可以这样做吗?)

所以在我的控制器中我有这个...“previousaction”帖子到此页面。

if params[:home_value] == "Select One"
    flash.now[:home_value] = "Please select a home value"
    render "previousaction"
else
    #set controller vars.. render this action as normal
end

在视图中:

<% if home_value %>
  <h6 id="notice" style="color:red;padding-bottom:5px;"><%= home_value %></h6>
  <%= label_tag "Estimated Home Value", "Estimated Home Value", :style => "color:red;"%><br/>
<% else %>
  <%= label_tag "Estimated Home Value", "Estimated Home Value" %><br/>
<% end %>

但是在尝试加载控制器操作(第一次)时出现此错误:

未定义的局部变量或方法“home_value”

提示:)

对于戴夫:

在之前的操作/视图中,我使用 flash[:notice ]像这样:

if params[:zip_code].length != 5
  flash.now[:notice] = "Invalid zipcode. Please enter your 5-digit zipcode"
  render "firstpage"
else 

然后在视图中

<% if notice %>

flash[:notice]是rails的特殊flash键吗?

So I'm doing some basic validation and trying to add an arbitrary key to the flash hash (you can do that right?)

So in my controller I have this... "previousaction" posts to this page.

if params[:home_value] == "Select One"
    flash.now[:home_value] = "Please select a home value"
    render "previousaction"
else
    #set controller vars.. render this action as normal
end

And in the view:

<% if home_value %>
  <h6 id="notice" style="color:red;padding-bottom:5px;"><%= home_value %></h6>
  <%= label_tag "Estimated Home Value", "Estimated Home Value", :style => "color:red;"%><br/>
<% else %>
  <%= label_tag "Estimated Home Value", "Estimated Home Value" %><br/>
<% end %>

But I get this error when trying to load the controller action (the first time):

undefined local variable or method `home_value'

Tips appreciated :)

For Dave:

In a previous action/view I use flash[:notice] like this:

if params[:zip_code].length != 5
  flash.now[:notice] = "Invalid zipcode. Please enter your 5-digit zipcode"
  render "firstpage"
else 

and then in the view

<% if notice %>

Is flash[:notice] a special flash key for rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-12-15 06:53:51

home_value 不会神奇地与 flash 关联,它只是被视为局部变量;尝试直接使用 :home_value 访问 flash 哈希。

也就是说,使用任意密钥的目的是什么?

home_value won't magically be associated with the flash, it's just being treated as a local variable; try accessing the flash hash with :home_value directly.

That said, what's the purpose of using an arbitrary key?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文