2016-08-02 48 views
-1

我有一个红宝石轨混合代码 我有一个形式与选择选项,我想成为一个必需的,我想验证。如果用户不选择任何我想验证的错误信息。 但是,我尝试从互联网上复制过去的代码,我是ruby rails的新手,但仍然没有错误消息。 我还要检查,我使用“必要”,或者如果我使用“validates_presence_of”不作区别,因为它是一个提交表单(我认为)选择所需的选项红宝石轨道

test_filteR_form.rb

class TestFilterForm < ApplicationForm 

    attribute :model,     String 
    validates_presence_of :model 
end 

.html.erb

<%= f.input :fill_form_error_message,:as => :hidden, :input_html => { :value =>I18n.t('test.fill_form_error') } %> 
    <%= f.input :model, label: I18n.t('test.filters.model'), autofocus: true, input_html: {class: 'input-xlarge chosen-select' }, collection: TestType.options_for_select, include_blank: true %> 

“/>

控制器

def paginate 
    @test_form = TestForm.new(params) 
    unless @test_form.valid? 
     @model = params[:test_filter_form][:model] 
     @h_model = @model.pluralize + 'H' 
     @history, _query, @test_fields = TestQueryService.search!(params) 

     session[:test_query] = _query 
     session[:test_klass] = @model 
    else 
     format.json { render :json => { :error => @test_form.errors.full_messages }, :status => 422 } 
    end 

js.coffee

$contentDiv.on 'ajax:error', 'form[data-value]', (event, xhr, status, error) -> 
    data = $.parseJSON(xhr.responseText) 
    $result = $(@).parents('tr').find('[data-result]') 
    controller.resultUpdateError xhr.status, $result.data('result'), data, $(@) 
    # Hide row loading spinner 
    $(@).parents('tr').find('span[role="result_update_spinner"]').hide() 
    # Hide saved form 
    $(@).parents('tr').find('.saved_form').hide() 


    resultUpdated: (result, data, $form) -> 
    if data.flash != undefined 
     # Sets a sucess message on page top 
     flash data.flash.type, data.flash.message 

    # Sets a success message on row 
    $fieldForm = $form.parents('tr').find(".messages") 
    $fieldForm.find('.controls').empty() 
    $fieldForm.find('.control-group .controls').css('color', 'green').append @_inlineMessage("Gravado com sucesso") 

    # Hide success message after some time 
    setTimeout ((self) -> 
     -> 
     $fieldForm.find('.control-group .controls').empty() 
     return 
    )(this), 4000 

回答

0

你既然是动态创建的选择框,然后必须有选择默认值不为零,所以没有变化见过你可以手动创建的选择是这样的:

<div class="form-group"> 
      <%= f.label :select_user_country %><br/> 
      <select class="form-control select2" name="user[country_id]"> 
      <option value="" selected disabled>Select a Country</option> 
      <%@countries.each do |country|%> 
       <option value="<%=country.id%>"><%=country.name%></option> 
      <%end%> 
      </select> 
     </div>