Rails 错误消息没有显示?

msm*_*114 0 ruby-on-rails

我是 Rails 的新手……但我正在尝试自己做一个应用程序来“练习”我学到的东西。

我有一个带有模型验证的新表单,但没有显示错误消息。这是我所拥有的:

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

_form.html.erb('New' 的表格) (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 %>
Run Code Online (Sandbox Code Playgroud)

我有点困惑为什么这不起作用?现在,当我点击创建按钮时,它会闪烁,但没有错误消息。我可以确认模型正在使用验证,因为如果我尝试执行Seed.create()并检查消息,它确实不起作用......所以我有点困惑?

据我所知,这.any?并没有发生,因为如果我!对该声明进行了操作,它至少会显示0 messages.

小智 6


您没有看到任何错误消息,因为form_with标签生成的所有表单都是remote: true默认的,并向服务器发送 xhr(ajax) 请求。如果您想查看错误消息,就您当前的设置而言,您必须添加local: true至此使提交正常。
更换

<%= form_with model: @seed, class: "form-horizontal" do |f| %>
Run Code Online (Sandbox Code Playgroud)

和

<%= form_with model: @seed,  local: true, class: "form-horizontal" do |f| %>
Run Code Online (Sandbox Code Playgroud)

会做的伎俩。希望这会有所帮助。