Ruby on Rails如何显示验证错误消息?

时间:2016-05-09 03:46:12

标签: ruby-on-rails ruby validation ruby-on-rails-4

我的代码正在运行,但它不显示错误消息。我需要显示创建新预订无效,因为table_number,@ requested_time和date已被预订。

reservations_controller.rb

def create
  if !current_user.try(:admin?)
    @user = User.find(params[:user_id]) 
    @reservation = current_user.reservations.build(reservations_params)
    @reservation.user = @user
    @reservation.save
    redirect_to @user
  else
    @user = User.find(params[:user_id]) 
    @reservation = current_user.reservations.build(reservations_params)
    @reservation.user = @user
    @reservation.save
    redirect_to "/lists/reservationlist"
  end
end

reservation.rb

class Reservation < ActiveRecord::Base

  belongs_to :user 
  belongs_to :customer
  belongs_to :table
  belongs_to :payment

  validates :table_id, uniqueness: { :scope => [:requested_date, :requested_time], :message => "Reservation is not available" }
end

new.html.erb

<br>
<%= form_for [@user, @reservation] do |f| %>
  <% if current_user.try(:admin?) %>
    <label>Client Name:</label><p &nbsp;></p>
    <span class="login">
      <%= f.text_field :client_name %><br>
    </span>
    <label>Client Address:</label><p &nbsp;></p>
    <span class="login">
      <%= f.text_field :client_address %><br>
    </span>
    <label>Client Contact:</label><p &nbsp;></p>
    <span class="login">
      <%= f.text_field :client_contact %><br><br>
    </span>
  <% end %>

  <label>Table Number:</label><p &nbsp;></p>
  <span class="table">
    <%= f.select(:table_id, options_for_select( Table.all.map{ |g| [g.table_number, g.id] } ), {:class => "styled-select"}) %><br>
  </span>

  <br><label>Number of Guest:</label><p &nbsp;></p>
  <span class="table">
    <%= f.select(:guest_size, options_for_select(1..6), {:class => "styled-select"}) %><p &nbsp;><br>
  </span>

  <label>Reservation date </label><p &nbsp;>      
  <div>
    <span class="datetime">
      <%= f.date_select :requested_date, order: [:month, :day, :year],:class => "styled-select"%><br><br>
    </span>
  </div>

  <label>Reservation Time</label><p &nbsp;>        
  <div>
    <span class="datetime">
      <%= f.time_select :requested_time,  ampm: true , minute_step: 30, start_hour: 8, end_hour: 22, :class => "styled-select" %>
    </span>
  </div><br><br> <!-- ,{ampm: true} -->

  <label>Reservation fee:</label><p &nbsp;></p>
  <div>
    <span class="datetime">
    <%= f.select :payment_id, Payment.all.map{ |j| [j.pay_reservation, j.id] }, :class => "styled-select"%><br><br>
    </span>
  </div>
  <div class="field">   
    <br><div class="actions"><%= f.submit "Confirm Reservation", class: "btn-style" %></div><br>  
  </div>
<% end %>

2 个答案:

答案 0 :(得分:0)

Try adding these lines

def create
    if !current_user.try(:admin?)
    @user = User.find(params[:user_id]) 
    @reservation = current_user.reservations.build(reservations_params)
    @reservation.user = @user
     **if @reservation.valid?
       @reservation.save
       redirect_to @user
     else
       # code to redirect to same page
     end**
    else
            @user = User.find(params[:user_id]) 
            @reservation = current_user.reservations.build(reservations_params)
            @reservation.user = @user
            @reservation.save
            redirect_to "/lists/reservationlist"
    end
end

答案 1 :(得分:0)

您的控制器中存在大量重复内容,并且在保存记录时不会检查是否存在错误:

def create
  @reservation = current_user.reservations.build(
    reservations_params.merge(user: User.find(params[:user_id]))
  )

  if @reservation.save
    redirect_to current_user.admin? ? '/lists/reservationlist' : @user
  else
    render :edit # re-render the edit form if there was an error
  end
end

edit视图中,您需要添加以下内容:

<% if @reservation.errors.any? %>
  <ul>
    <% @reservation.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>