I have a model BookingPost that has many Reservations. In booking_posts/show.html.erb I have:
<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_posts_controller:
# GET /booking_posts/1
# GET /booking_posts/1.json
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
reservations_controller:
# GET /reservations/new
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)
respond_to do |format|
if @reservation.save
@saved_reservation = @reservation
format.html { redirect_to :back, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: @reservation }
ReservationMailer.fresh_message(@saved_reservation, @email).deliver_now
else
format.html {redirect_to :back
flash[:info] = @reservation.errors.full_messages do |m|
m
end}
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
But I can't see any validation errors, even like presence: true. Seems like I don't have correct @reservation, because I have a deal with Array in my form_for. Help me please to solve this issue, thanks!
The reason you are not seeing validation errors is because you are issuing a redirect_to :back
when validations fails instead of rendering :new action which already includes any errors you might have inside the @reservation instance object.
Also you were trying to add the error messages into the flash message while trying to display them using the @reservation object.
this should be the correct else
clause
else
format.html { render :new }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.