我是铁杆新手......但是我试图自己做一个应用程序来练习"练习"我学到了什么。
我有一个带有模型验证的新表单,但错误消息没有显示。这就是我所拥有的:
seed.rb
(型号)
class Seed < ApplicationRecord
validates :name, presence: true
validates :category, presence: true
validates :latin, presence: true
validates :maturity, presence: true
validates :sun, presence: true
validates :sow, presence: true
validates :cycle, presence: true
validates :description, presence: true, length: { minimum: 5, maximum: 500 }
mount_uploader :seedimage, SeedImageUploader
end
seed_controller.rb
(财务主任)
class SeedsController < ApplicationController
def index
@seeds = Seed.all
end
def new
@seed = Seed.new
end
def create
@seed = Seed.new(seed_params)
if @seed.save
redirect_to seeds_path
else
render 'new'
end
end
def edit
end
def update
@seed = Seed.find(params[:id])
if @seed.update(seed_params)
redirect_to @seed
else
render 'edit'
end
end
def show
@seed = Seed.find(params[:id])
end
def destroy
@seed = Seed.find(params[:id])
@seed.destroy
redirect_to seeds_path
end
def seed_params
params.require(:seed).permit(:name, :category, :latin, :maturity, :sun, :sow, :cycle, :description, :seedimage)
end
end
_form.html.erb
(表格&#39;新&#39;)(new.html.erb
只有<% render 'form' %>
<%= form_with model: @seed, class: "form-horizontal" do |f| %>
<% if @seed.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@seed.errors.count, "error") %> prohibited
this seed from being saved:
</h2>
<ul>
<% @seed.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: "Seed Name" %>
</div>
<div class="form-group">
<%= f.label :category %>
<%= f.text_field :category, class: "form-control", placeholder: "Category: 'beans'" %>
</div>
<div class="control-label">
<%= f.label :latin %>
<%= f.text_field :latin, class: "form-control", placeholder: "Latin Name" %>
</div>
<div class="form-group">
<%= f.label :maturity %>
<%= f.number_field :maturity, class: "form-control", placeholder: "Maturity Time" %>
</div>
<div class="form-group">
<%= f.label :sun %>
<%= f.select(:sun, options_for_select([['Full Sun'], ['Partial Sun'], ['Full Shade']]), {}, { class: "custom-select"}) %>
</div>
<div class="form-group">
<%= f.label :sow %>
<%= f.text_field :sow, class: "form-control", placeholder: "Plant Indoors/Sow Outdoors/etc.." %>
</div>
<div class="form-group">
<%= f.label :cycle %>
<%= f.text_field :cycle, class: "form-control", placeholder: "Annual/Perennial/etc.." %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, size: "60x12", class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :seedimage %>
<%= f.file_field :seedimage, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Create", class: "btn btn-primary btn-lg" %>
</div>
<% end %>
我有点困惑为什么这不起作用?现在,当我点击创建按钮时,它会闪烁,但没有错误消息。我可以确认该模型正在使用验证,因为如果我尝试执行Seed.create()
并检查消息,那它确实不起作用....所以我有点困惑?
据我所知,.any?
并未发生,因为如果我对该声明!
,则至少会显示0 messages
。
答案 0 :(得分:2)
您没有看到任何错误消息,因为form_with
标记生成的所有表单默认为remote: true
,并向服务器发送xhr(ajax)请求。如果您想查看错误消息,请在当前设置中添加local: true
,以使提交正常。
更换
<%= form_with model: @seed, class: "form-horizontal" do |f| %>
与
<%= form_with model: @seed, local: true, class: "form-horizontal" do |f| %>
会做到这一点。希望这会有所帮助。