Verification messages after redirection

We have a rating form for a specific restaurant in our view / restaurants / show.html.erb. If there is a validation error, we are redirected back to views / restaurants / show.html.erb, but the validation messages are not displayed. We found out that this is because we are losing messages using redirect_to (@restaurant) in our CreateController action. But how can we return without redirecting?

Thanks!

+4
source share
5 answers

This is how I solved it. (Note that in the future I will obviously just include only the most relevant lines.)

A model can have multiple checks and even methods that can report multiple errors.

class Order < ActiveRecord::Base validates :name, :phone, :email, :presence => true def some_method(arg) errors.add(:base, "An error message.") errors.add(:base, "Another error message.") end end 

In addition, the action of the controller can set flash messages. Finally, the user can enter data in the input fields, and we want him to continue through redirect_to .

 class OrdersController < ApplicationController def create @order = Order.new(params[:order]) respond_to do |format| if @order.save session.delete(:order) # Since it has just been saved. else session[:order] = params[:order] # Persisting the order data. flash[:notice] = "Woohoo notice!" # You may have a few flash messages flash[:alert] = "Woohoo alert!" # as long as they are unique, flash[:foobar] = "Woohoo foobar!" # since flash works like a hash. flash[:error] = @order.errors.to_a # <-- note this line format.html { redirect_to some_path } end end end end 

Depending on your installation, you may or may not need to save model data, such as an order, per session. I did this to transfer the data back to the original controller and thereby was able to adjust the order again.

In any case, in order to display the actual error and flash messages, I did the following (in views/shared/_flash_messages.html.erb , but you could do this in application.html.erb or wherever it makes sense for your application ) And this is thanks to this line flash[:error] = @order.errors.to_a

 <div id="flash_messages"> <% flash.each do |key, value| # examples of value: # Woohoo notice! # ["The server is on fire."] # ["An error message.", "Another error message."] # ["Name can't be blank", "Phone can't be blank", "Email can't be blank"] if value.class == String # regular flash notices, alerts, etc. will be strings value = [value] end value.each do |value| %> <%= content_tag(:p, value, :class => "flash #{key}") unless value.empty? %> <% end %> <% end %> </div><!-- flash_messages --> 

To be clear, regular flash messages, such as notifications, warnings, etc., will be strings, however errors will be arrays since the aforementioned call was errors.to_a

+4
source

You can pass your error in a flash message

 flash[:error] = @restaurant.errors 

After you need to display it in your redirect

+2
source

Here's how I still did the redirection:

Just before redirecting verification errors to the controller vaults, do the following:

 if @restaurant_rating.save redirect_to @restaurant, :notice => "Successfully added rating to restaurant." else flash[:error] = @restaurant_rating.errors redirect_to @restaurant, :alert => "There were errors to add rating to restaurant. " end 

Then, in your rating form, you assign errors to the rating object immediately before the form is visualized:

 - flash[:error].messages.each {|error| @restaurant_rating.errors.add(error[0], error[1][0]) } if flash[:error] = simple_form_for @restaurant_rating do |f| .... 
+2
source

You can use render instead of redirect_to

 render :action => "show" 

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

+1
source

After you explained in the comment, you need to configure

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

with this line

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

Source: https://habr.com/ru/post/1309565/


All Articles