如何设置关联模型的错误信息?
How to set up error messages of associated model?
大家好 rails 社区!
我有 booking_post 个模型 has_many 预订。
class BookingPost < ApplicationRecord
has_many :reservations, dependent: :destroy
end
全部保留 belongs_to booking_post 并进行一些验证
class Reservation < ApplicationRecord
belongs_to :booking_post
before_save { self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :name, :email, :phone_number, :start, :end, presence: true
end
我的路线是:
resources :booking_posts do
resources :reservations, only: [:new, :create]
end
方法:
class BookingPostsController < ApplicationController
def show
@booking_picture = @booking_post.booking_pictures.build
@booking_pictures = @booking_post.booking_pictures
@reservation = @booking_post.reservations.build
@reservations = @booking_post.reservations
end
end
class ReservationsController < ApplicationController
def new
@reservation = Reservation.new
end
def create
@booking_post = BookingPost.find(params[:booking_post_id])
@email= User.where(admin: true).first.email
@reservation = @booking_post.reservations.build(reservation_params)
if @reservation.save
@saved_reservation = @reservation
redirect_to :back
flash[:notice] = 'Reservation was successfully created.'
ReservationMailer.fresh_message(@saved_reservation, @email).deliver_now
else
redirect_to @booking_post
flash[:info] = @reservation.errors.full_messages do |m|
m
end
end
end
end
我想在 booking_posts/show.html.erb form_for @reservation 上创建,并在此页面上呈现 @reservation 的错误。当我创建有效的@reservation 时,我在 booking_posts/show.html.erb 上看到成功的闪现消息,但出现了无效的 @reservation,没有任何错误闪现消息。
form_for @reservation on booking_posts/show.html.erb:
<div class="card-action">
<%= form_for([@reservation.booking_post, @reservation], html: {multipart: true}, class: "col s12") do |f| %>
<% if @reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@reservation.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col s6">
<%= f.label :start %>
<%= f.date_field :start, placeholder: "start time", class: "datepicker" %>
</div>
<div class="col s6">
<%= f.label :end %>
<%= f.date_field :end, placeholder: "end time", class: "datepicker" %>
</div>
<div class="col s6">
<%= f.label :reservation_time %>
<%= f.time_field :reservation_time, placeholder: "time", class: "timepicker", id: "timepicker", type: "time" %>
</div>
<div class="input-field col s6">
<%= f.label :name %>
<%= f.text_field :name, class: "validate" %>
</div>
<div class="input-field col s6">
<%= f.label :email %>
<%= f.text_field :email, class: "validate" %>
</div>
<div class="input-field col s6">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: "validate" %>
</div>
<div class="waves-effect waves-light btn">
<%= f.submit t(:submit_reservation)%>
</div>
<% end %>
<br>
</div>
我想在@booking_post 页面上呈现@reservation 的错误消息
(在 booking_post_path 中,不在 new_reservation_path 或其他任何地方)。我该怎么做?
感谢解决方案
在你的 else 块中,请像这样更新它
flash[:notice] = @reservation.errors.full_messages.to_sentence
redirect_to @booking_post
大家好 rails 社区!
我有 booking_post 个模型 has_many 预订。
class BookingPost < ApplicationRecord
has_many :reservations, dependent: :destroy
end
全部保留 belongs_to booking_post 并进行一些验证
class Reservation < ApplicationRecord
belongs_to :booking_post
before_save { self.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :name, :email, :phone_number, :start, :end, presence: true
end
我的路线是:
resources :booking_posts do
resources :reservations, only: [:new, :create]
end
方法:
class BookingPostsController < ApplicationController
def show
@booking_picture = @booking_post.booking_pictures.build
@booking_pictures = @booking_post.booking_pictures
@reservation = @booking_post.reservations.build
@reservations = @booking_post.reservations
end
end
class ReservationsController < ApplicationController
def new
@reservation = Reservation.new
end
def create
@booking_post = BookingPost.find(params[:booking_post_id])
@email= User.where(admin: true).first.email
@reservation = @booking_post.reservations.build(reservation_params)
if @reservation.save
@saved_reservation = @reservation
redirect_to :back
flash[:notice] = 'Reservation was successfully created.'
ReservationMailer.fresh_message(@saved_reservation, @email).deliver_now
else
redirect_to @booking_post
flash[:info] = @reservation.errors.full_messages do |m|
m
end
end
end
end
我想在 booking_posts/show.html.erb form_for @reservation 上创建,并在此页面上呈现 @reservation 的错误。当我创建有效的@reservation 时,我在 booking_posts/show.html.erb 上看到成功的闪现消息,但出现了无效的 @reservation,没有任何错误闪现消息。
form_for @reservation on booking_posts/show.html.erb:
<div class="card-action">
<%= form_for([@reservation.booking_post, @reservation], html: {multipart: true}, class: "col s12") do |f| %>
<% if @reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@reservation.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col s6">
<%= f.label :start %>
<%= f.date_field :start, placeholder: "start time", class: "datepicker" %>
</div>
<div class="col s6">
<%= f.label :end %>
<%= f.date_field :end, placeholder: "end time", class: "datepicker" %>
</div>
<div class="col s6">
<%= f.label :reservation_time %>
<%= f.time_field :reservation_time, placeholder: "time", class: "timepicker", id: "timepicker", type: "time" %>
</div>
<div class="input-field col s6">
<%= f.label :name %>
<%= f.text_field :name, class: "validate" %>
</div>
<div class="input-field col s6">
<%= f.label :email %>
<%= f.text_field :email, class: "validate" %>
</div>
<div class="input-field col s6">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: "validate" %>
</div>
<div class="waves-effect waves-light btn">
<%= f.submit t(:submit_reservation)%>
</div>
<% end %>
<br>
</div>
我想在@booking_post 页面上呈现@reservation 的错误消息 (在 booking_post_path 中,不在 new_reservation_path 或其他任何地方)。我该怎么做?
感谢解决方案
在你的 else 块中,请像这样更新它
flash[:notice] = @reservation.errors.full_messages.to_sentence
redirect_to @booking_post