OK。過去幾周我一直在學習Rails,我真的是一個小菜鳥。Rails 4驗證問題:表單驗證無效後提交
我正在致力於酒店預訂系統。這個想法是加載一個表單,驗證字段,並保存數據。如果沒有任何驗證錯誤,則保留已妥善保存。但是,如果表單顯示一些驗證錯誤,提交按鈕停止工作(我的意思是,我可以修復文本,所以驗證錯誤不會彈出,但一旦修復,我就無法提交新數據)。
這是型號:
class Reserva < ActiveRecord::Base
validates :fecha_ingreso, :fecha_salida, presence: { message: " es un campo obligatorio" },
format: { with: /\d{2}\/\d{2}\/\d{4}/ , message: "Debe utilizar formato dd/mm/aaaa" }
validates :canti_hues, :tipo_hab, :email, :peticion, presence: { message: " es un campo obligatorio" }
validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
end
這是控制器:
before_action :set_reserva, only: [:show, :edit, :update, :destroy]
# GET /reservas
# GET /reservas.json
def index
if user_signed_in?
@reservas = Reserva.all
else
redirect_to :controller => 'home', :action => 'ingresar'
end
end
# GET /reservas/1
# GET /reservas/1.json
def show
end
# GET /reservas/new
def new
@reserva = Reserva.new
end
# GET /reservas/1/edit
def edit
end
# POST /reservas
# POST /reservas.json
def create
@reserva = Reserva.new(reserva_params)
respond_to do |format|
if @reserva.save
format.html { redirect_to @reserva, notice: 'La reserva fue creada exitosamente.' }
format.json { render :show, status: :created, location: @reserva }
else
format.html { render :new }
format.json { render json: @reserva.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservas/1
# PATCH/PUT /reservas/1.json
def update
respond_to do |format|
if @reserva.update(reserva_params)
format.html { redirect_to @reserva, notice: 'Reserva was successfully updated.' }
format.json { render :show, status: :ok, location: @reserva }
else
format.html { render :edit }
format.json { render json: @reserva.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservas/1
# DELETE /reservas/1.json
def destroy
@reserva.destroy
respond_to do |format|
format.html { redirect_to reservas_url, notice: 'Reserva was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reserva
@reserva = Reserva.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reserva_params
params.require(:reserva).permit(:fecha_ingreso, :fecha_salida, :canti_hues, :tipo_hab, :email, :peticion)
end
end
而且這是在視圖中呈現的_form:
<%= form_for(@reserva) do |f| %>
<% if @reserva.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@reserva.errors.count, "error") %> encontrado:</h2>
<ul>
<% @reserva.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div align='center'>
<table border='1'>
<td width=300 align='center'>
<div class="field">
<%= f.label :fecha_ingreso %><br>
<%= f.text_field :fecha_ingreso %>
</div>
<div class="field">
<%= f.label :fecha_salida %><br>
<%= f.text_field :fecha_salida %>
</div>
<div class="field">
<%= f.label :cantidad_de_huespedes %><br>
<%= f.number_field :canti_hues %>
</div>
<div class="field">
<%= f.label :tipo_de_habitación %><br>
<%= f.text_field :tipo_hab %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :peticiones_especiales %><br>
<%= f.text_area :peticion %>
</div>
<div class="actions">
<%= f.submit %>
</div>
</div>
</td>
</table>
<% end %>
爲什麼我不能如果驗證錯誤已被修復,請提交? 謝謝你的回答!
它是否顯示控制檯中的任何內容,即JavaScript相關?當你說按鈕不起作用,你的意思是它不可點擊? – osman
請貼上相關'javascript' – DevMarwen
請貼上你得到的錯誤 –