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中。怎么可能请帮助我。
答案 0 :(得分:0)
@kiran
我看到你使用bootstrap。这里有一些简单的助手可以满足您的需求:
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:)