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?