I have trouble getting a flash message to show after a failed form submit.
Here is the form:
<%= 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 %>
And this is the part of the controller handling it:
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
The template file for the new action contains nothing but the form above.
I am currently using this code snippet (in application.html.erb) to display flash messages, and it works for the flash message after a successful save and redirecting to index:
<% flash.each do |k, v| %>
<div class="<%= k %>"><%= v %></div>
<% end %>
I have not been able to find a solution to this problem myself, so I’m hoping a more experienced pair of eyes will be able to spare what I assume to be just a few minutes to enlighten me.
Thanks!
P.S.: I have also tried a regular flash[:key => "value"] as well as render 'new', :flash => {:alert => "There was a problem when trying to save your question!"}
I believe that by this:
You meant this: