开发者

Rails flash.now hash with arbitrary key?

开发者 https://www.devze.com 2023-04-10 16:36 出处:网络
So I\'m doing some basic validation and trying to add an arbitrary key to the flash hash (you can do that right?)

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开发者_JAVA技巧 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号