表单提交后使用'render'时不显示Flash消息

mat*_*jay 0 forms ruby-on-rails-3

在提交失败的表单后,我无法收到要显示的Flash消息.

这是表格:

<%= form_for @question do |q| %>

  <p>
    <b>Your question</b><br />
    <%= q.text_field :title, :size => 48 %>
  </p>
  <p>
    <b>Add more details</b><br />
    <%= q.text_area :body, :size => "80x10" %>
  </p>
  <p>
    <%= q.submit %>
  </p>
<% end %>
Run Code Online (Sandbox Code Playgroud)

这是处理它的控制器的一部分:

def create
    @question = current_user.questions.build(params[:question])
    if @question.save
      redirect_to questions_path, :flash => { :success => "Your question was saved!" }
    else
      flash.now[:alert => "There was a problem when trying to save your question!"]
      render 'new'
    end
  end
Run Code Online (Sandbox Code Playgroud)

该操作的模板文件只new包含上面的表单.

我目前正在使用此代码段(in application.html.erb)来显示Flash消息,并且在成功保存并重定向到index以下内容后,它适用于Flash消息:

  <% flash.each do |k, v| %>
      <div class="<%= k %>"><%= v %></div>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

我自己一直无法找到解决这个问题的方法,所以我希望有一双更有经验的眼睛可以省去我认为只需几分钟的启发.

谢谢!

PS:我也尝试了常规flash[:key => "value"]以及render 'new', :flash => {:alert => "There was a problem when trying to save your question!"}

dan*_*neu 5

我相信这个:

flash.now[:alert => "There was a problem when trying to save your question!"]
Run Code Online (Sandbox Code Playgroud)

你的意思是:

 flash.now[:alert] = "There was a problem when trying to save your question!"
Run Code Online (Sandbox Code Playgroud)