简单表单输入:当引发错误时,集合不突出显示字段

时间:2015-08-08 08:36:15

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个简单形式的问题,它没有突出显示错误的字段。

search_controller.rb

    class SearchesController < ApplicationController
  before_action :set_search, only: [:show, :edit, :update, :destroy]

  layout 'admin'

  def index
    @searches = Search.includes(:brand, :search_index, :price_ranges)
  end

  def show
  end

  def new
    @search = Search.new
  end

  def edit
  end

  def create
    @search = Search.new(search_params)

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

  def update
    respond_to do |format|
      if @search.update(search_params)
        format.html { redirect_to searches_url, notice: 'Search was successfully updated.' }
        format.json { render :show, status: :ok, location: @search }
      else
        format.html { render :edit }
        format.json { render json: @search.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @search.destroy
    respond_to do |format|
      format.html { redirect_to searches_url, notice: 'Search was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_search
      @search = Search.find(params[:id])
    end

    def search_params
      params.require(:search).permit(:brand_id)
    end
end

search.rb

class Search < ActiveRecord::Base    
validates_presence_of :brand

_form.html.erb

<%= simple_form_for(@search) do |f| %>
  <%= f.error_notification %>
  <%= f.input :brand_id, collection: Brand.all.order(:name), prompt: 'Select brand' %>
 <% end %>

enter image description here 我希望品牌领域与关键字领域相同。 我已检查 validates_presence_of品牌是否会引发错误及其剂量,但不会突出显示该字段。

0 个答案:

没有答案