选择所需的选项Ruby Rails

时间:2016-08-02 14:05:02

标签: ruby-on-rails ajax

我有混合的ruby rails代码 我有一个带有选择选项的表单,我想成为必需的,我想验证。如果用户没有选择任何我想验证的错误消息。 但是,我尝试从互联网上复制过去的代码,我是ruby rails的新代码,但我仍然没有错误消息。 我还检查我是否使用了“#required;' ,或者如果我使用'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 %>

&#34; /&GT;

控制器

 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

1 个答案:

答案 0 :(得分: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>