0

フォームを作成するためにsimple_formgemを使用しようとしています。また、スタイリングにTwitterブートストラップを使用しているため、simple_formが硬すぎて、やりたいことを実行できない場合があります(常にではありません)。

検証に失敗するはずのフォームを送信しています。ただし、ページを再度レンダリングすると、入力の近くにエラーメッセージは表示されず、ページは半分しかレンダリングされません。

コントローラコード:(注:私は基本的に同じものとして表示/編集アクションを使用しています)

def update
  @contact = current_resource
  authorize! :update, @contact

  respond_to do |format|
    if @contact.update_attributes(params[:customer_relationship_contact])
      format.html { redirect_to customer_relationship_contact_url(@contact), notice: "#{@contact.to_s} was successfully updated." }
      format.json { head :no_content }
    else
      format.html { render :edit }
      format.json { render json: @contact.errors, status: :unprocessable_entity }
    end
  end
end

def show
  @contact = current_resource
  authorize! :show, @contact

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @contact }
  end
end

ビュー/フォームコード

<%= simple_form_for @contact, :html => {:novalidate => true} do |f| %>
<%= f.error_notification %>
<fieldset>

  <legend>Contract</legend>
  <div class="controls controls-row">
    <div class="span3">
      <div class="controls controls-row">
        <%= f.label :tax_id_number, 'Social Security No.' %>
        <%= f.text_field :tax_id_number, class: 'span12', placeholder: 'Social Security No.' %>
      </div>
    </div>
  </div>
  </div>
</fieldset>

<div class="form-actions">
  <%= f.button :submit, :class => 'btn-primary' %>
  <%= link_to "Cancel", customer_relationship_contacts_path, :class => 'btn' %>
</div>

モデルの検証

    validates :tax_id_number, format: { with: /\A[0-9]+\z/, message: "Please enter only numbers without dashes." }, allow_blank: true
4

1 に答える 1

0

ここでの問題は、ビューでf.labelandを使用していることですf.text_field-シンプルなフォームはシンプルになるように構築されているため、必要なものは次のとおりですf.input-この場合:

<%= f.input :tax_id_number, label: 'Social Security No.', placeholder: 'Social Security No.' %>

ここで何が起こるか -f.input入力クラスでサラウンド div を作成します。その div 内には、ラベルと入力があります。エラーが発生した場合、単純なフォームはクラスfield_with_errorsを周囲の div に追加し、クラスを使用した入力の後にメッセージを含むスパンを追加しますerror。使わないとこうならないf.input

残っているのは、css でスタイルを設定することだけです。

于 2016-03-08T11:53:03.750 に答える