I am validating the associated model, first:

class Bike < ApplicationRecord has_many :stata_fuels, dependent: :destroy end 

and related

 class StataFuel < ApplicationRecord belongs_to :bike validates :odo_delta, presence: { message: "введите пробег между заправками" } validates :refueling, presence: { message: "введите кол-во заправленного топлива" } validates :price_fuel, presence: { message: "введите сумму оплаты за топливо" } end 

When validating a Bike model, I get error messages from a variable using the usual method @bike.errors method. The bikes_controller controller code part is below:

  def create @bike = Bike.new(bike_params) if @bike.save redirect_to bikes_path else render 'new' end 

When validating the FuelStata model, validation works, i.e., if the data fields are empty, the database is not written, but I cannot receive error messages in the variable.

Below is the stata_fuels_controller controller part:

 class StataFuelsController < ApplicationController before_action :set_view_stata_fuel def new @bike = Bike.find(params[:bike_id]) @stata_fuel = StataFuel.new end def create @bike = Bike.find(params[:bike_id]) if @bike.stata_fuels.create(fuel_params) redirect_to new_bike_stata_fuel_path else render 'new' end end 

and itself:

 <h2> Fuel statistics page </h2> <p id="notice"><%= notice %></p> <%= @bike.stata_fuels.errors %> <%= render 'form_fuel_create', stata_fuel: @stata_fuel %> <%= link_to 'Back', bikes_path %> 

on line with @ bike.stata_fuels.errors Rails give the error undefined method `stata_fuel 'for #.

Tell me, please, how can I get error messages in a variable, so that later they can be displayed in a message? Perhaps there are nuances of error messages for related objects?

2 answers 2

First in the controller:

 @stata = @bike.stata_fuels.create(fuel_params) 

Potov in the view:

 <%= @stata.errors %> 
  • The problem is solved, thank you all! - Yar-ua

Since stata_fuels is a collection (collection) and not an item itself, it should not give a list of errors.

To check the new occurrences of the dependent (has_many) set in the item, you need to set the validate property to true :

 class Bike has_many :stata_fuels, dependent: :destroy, validate: true end 

and check the item itself:

 @bike.valid? @bike.errors 

if you want to check not only new, but all dependent entries, then use the validate directive in the model instead of the validate property.

 class Bike has_many :stata_fuels, dependent: :destroy validates_associated :stata_fuels end 
  • Completed the above, still fails to get a validation error message. on @ bike.errors gives an empty hash. I tried through rails console - the result is the following: => # <ActiveModel :: Errors: 0xb8a0290 @base = # <Bike id: 1, name: "Viper VXR 250", user_name: "Yar", year: 2014, color: "black ", created_at:" 2016-10-04 20:31:26 ", updated_at:" 2016-10-04 20:31:26 ">, @ messages = {}, @details = {}> - Yar-ua
  • And if you enter another variable:. @ stata_fuel_err = StataFuel.new (fuel_params) @ stata_fuel_err.valid? then in the view <% = @ stata_fuel_err.errors.messages%> <br/> I get validation messages! {: bike => ["must exist"],: odo_delta => ["enter the mileage between the refueling"],: refuses => ["enter the number of the filled fuel"] ... - Yar-ua
  • @ Yar-ua means you do something permanently in the controller if you need to add another operation in addition .... check - Mal Skrylev
  • @ Yar-ua and then, what options did you try, both? - Mal Skrylev
  • Skrylev, yes. I tried both options. When checking bike.valid? and bike.errors in the controller and when outputting to the view output an empty array. The bike object itself did not receive validation errors, since the shared bike validated without errors. And collection errors on bike.errors were not displayed. - Yar-ua