Ruby on Rails - simple_form_for如何显示每个字段的错误

时间:2018-02-09 13:37:42

标签: html ruby-on-rails error-handling simple-form-for

我想在每个字段下显示错误,我的html.erb如下:

<%= simple_form_for [:admin, @municipality, @subdomain], :html => {:class => ''} do |f| %>

    <%= f.input :subdomain, hint: 'Dominio senza senza http://', error: true%>

    <%= f.input :subdomain_type, as: :select, collection: [['Sito','S'],['Api','A']] %>
    <%= f.input :active %>
    <%= f.input :draft,
                hint: 'Si applica ai siti e indica se il sito è una bozza interna (e quindi non deve essere raggiungibile dai motori di ricerca)',
                label: 'È una bozza' %>
    <div class="form-actions">
      <%= f.button :submit, :class => 'btn-primary', :value => 'Salva' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  admin_municipality_subdomains_path, :class => 'btn' %>
    </div>
<% end %>
@subdomain 变量中的

我有“错误”属性,它返回错误的完整列表,问题是:如何在子域字段下显示它?

编辑,控制器代码

class Admin::SubdomainsController < InheritedResources::Base
  layout 'admin'
  before_filter :authenticate_admin!
  before_filter :set_page_title
  load_and_authorize_resource
  actions :all, except: [:show]

  belongs_to :municipality
  add_breadcrumb Subdomain.model_name.human(:count => 2).titleize, :admin_municipality_subdomains_path

  def index
    @subdomain = Subdomain.new
    index!
  end

  def create
    create! {
      @municipality = Municipality.find(params[:municipality_id])
      admin_municipality_subdomains_path
    }
  end

  def destroy
    @municipality = Municipality.find(params[:municipality_id])
    @subdomain = Subdomain.find(params[:id])
    destroy!
  end

  private
  def permitted_params
    params.permit(:subdomain => [:active, :subdomain, :blocked, :subdomain_type, :draft])
  end

  def set_page_title
    @page_title = Subdomain.model_name.human(:count => 2).titleize
  end
end

1 个答案:

答案 0 :(得分:0)

我想你可以添加像

这样的东西
<%= @subdomain.errors if @subdomain.errors %>

下的

 <%= f.input :subdomain, hint: 'Dominio senza senza http://', error: true%>