簡體   English   中英

如何在Rails 4中的index.html.erb中顯示表單錯誤?

[英]How to show form errors in index.html.erb in Rails 4?

Suppliers_controller.rb

    class SuppliersController < ApplicationController
  before_action :set_supplier, only: [:show, :edit, :update, :destroy]

  # GET /suppliers
  # GET /suppliers.json
  def index
    @suppliers = Supplier.all
  end

  # GET /suppliers/1
  # GET /suppliers/1.json
  def show
  end

  # GET /suppliers/new
  def new

  end

  # GET /suppliers/1/edit
  def edit
  end

  # POST /suppliers
  # POST /suppliers.json

  def create
    @supplier = Supplier.new(supplier_params)

    respond_to do |format|
      if @supplier.save
        format.html { redirect_to :back, notice: 'Supplier was successfully created.' }
        format.json { render action: 'show', status: :created, location: @supplier }
      else
        format.html { render action: 'new'}
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /suppliers/1
  # PATCH/PUT /suppliers/1.json
  def update
    respond_to do |format|
      if @supplier.update(supplier_params)
        format.html { redirect_to @supplier, notice: 'Supplier was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /suppliers/1
  # DELETE /suppliers/1.json
  def destroy
    @supplier.destroy
    respond_to do |format|
      format.html { redirect_to suppliers_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_supplier
      @supplier = Supplier.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def supplier_params
      params.require(:supplier).permit(:name, :supplier_type)
    end
end

_form.html.erb

<%= simple_form_for @supplier do |f| %>

  <%= f.error_notification %>
  <%= f.input :name , placeholder:"Name" %>
  <br>

  <%= f.input :supplier_type , placeholder:"Supplier Type", label:"Supplier Type", :collection => ['Agent', 'Farmer'] ,:class => "supp" , :selected => 'Agent'%>
  <br><br>
  <div class="modal-footer">
  <%= f.button :submit ,value:"Add" , :class=>"btn btn-primary" , :remote => true  %>
 <button type="button" class="btn btn-white" data-dismiss="modal">Cancel</button> 

<% end %> 
</div>

Supplier.rb

    class Supplier
  include Mongoid::Document
  field :name, type: String
  field :supplier_type, type: String
  validates_presence_of :name, :supplier_type
  validates_uniqueness_of :name 
end

如何在index.html.erb中顯示表單錯誤。

注意:假設我已經在供應商表格中輸入了重復的值,那么它將響應控制器中的新操作並說出已經使用的名稱。 此消息將顯示在index.html.erb中。 怎么可能請幫助我。

@kiran

我看到您使用引導程序。 這里有一些簡單的幫助程序,您可以根據需要使用:

def errors_for(object)
    if object.errors.any?
        content_tag(:div, class: "panel panel-danger") do
            concat(content_tag(:div, class: "panel-heading") do
                concat(content_tag(:h4, class: "panel-title") do
                    concat "#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
                end)
            end)
            concat(content_tag(:div, class: "panel-body") do
                concat(content_tag(:ul) do
                    object.errors.full_messages.each do |msg|
                        concat content_tag(:li, msg)
                    end
                end)
            end)
        end
    end
end

只需將上面的代碼添加到application_helper中,您就可以使用

<%= errors_for(@user) %>

片段。 有一個不錯的stackoverflow :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM