【发布时间】:2017-10-20 03:56:36
【问题描述】:
我有一个模型 BookingPost,它有很多预订。 在 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_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
但我看不到任何验证错误,即使是存在:真。 似乎我没有正确的@reservation,因为我在 form_for 中处理了 Array。请帮我解决这个问题,谢谢!
【问题讨论】:
-
请贴出处理表单的控制器动作代码
-
对不起,我添加了它
标签: ruby-on-rails forms validation