views:

69

answers:

3

Hello world!

We have a form to submit ratings for a certain restaurant in a in our views/restaurants/show.html.erb. If there is a validation error we get redirected back to views/restaurants/show.html.erb but the validation messages do not appear. We figured out that this happens because we lose the messages using redirect_to(@restaurant) in our RatingController create action. But how can we get back without redirection?

Thanks!

A: 

After your clarification in the comment, you need to setup your

/app/views/layouts/application.html.erb

with this line

<%- flash.each do |name, msg| -%><%= content_tag :div, msg, :id => "flash_#{name}" %><%- end -%>
tommasop
A: 

You can pass your error on flash message

flash[:error] = @restaurant.errors

After you need display it in your redirect

shingara
Do you mean flash[:error] = @rating.errors? This is what I put in the RatingsController create action: if @rating.save ... else flash[:error] = @rating.errors format.html { redirect_to(@restaurant) } endAnd this is how I try to print them in views/restaurants/show.html.erb: <% form_for [@restaurant, Rating.new] do |f| %> <%= f.error_messages %> ...But nothing appears. Is there anything wrong? (I also tried @rating.errors).I'm new to this. Thanks!
You need to add this to your /app/views/layouts/application.html.erb<%- flash.each do |name, msg| -%><%= content_tag :div, msg, :id => "flash_#{name}" %><%- end -%>.In this way all your flash messages will be correctly rendered
tommasop
+1  A: 

You can use render instead of redirect_to

render :action => "show"

or set flash[:error], flash[:notice] again, because they automatically reseted

fl00r