フォームを作成するために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