简体   繁体   English

Select2 Rails表格重新填充

[英]Select2 rails form repopulation

I created a rails (v5) form with multiple select and collection_select elements. 我创建了带有多个select和collection_select元素的rails(v5)表单。 Then I use Select2-rails (v4.0.3) to allow a nice selection looking like tags. 然后,我使用Select2-rails(v4.0.3)允许进行类似标签的不错选择。 The search-options are pulled by ajax. 搜索选项由ajax提取。

It works fine until one presses the submit-button with missing required fields. 直到有人按下缺少必填字段的提交按钮,它才能正常工作。 Valid field-content has now been deleted from the field. 有效的字段内容现已从字段中删除。

Let me give some example-code: 让我给一些示例代码:

controller: 控制器:

...

def form
    if params[:form_request].nil?
        @form_request = FormRequest.new
    else
        @form_request = FormRequest.new(params[:form_request])
    end
end

def request_form
    @form_request = FormRequest.new(params[:form_request])

    if @form_request.valid?
        render :summary
    else
        render :form
    end
end

...

form: 形成:

...
<%= bootstrap_form_for(@form_request, url: '/form/request_form') do |f| %>
    <%= f.select :field, [], {label: 'Field label'} %>
    <%= f.submit "Submit form" %>
<% end %>

:field is for sure a writable field in the model (and data is set fine) :field确保模型中的可写字段(并且数据设置正确)

coffee-script: 咖啡脚本:

Query ->
   $("#form_request_from").select2({
       ajax: {
          url: func =(params) ->
             filter = params.term
             return "/data.json?filter=" + filter;
            ,
         dataType: 'json',
         processResults: processData
       },
       theme: 'bootstrap',
       placeholder: 'Enter data here'
  });

processData = (data) ->
    mapdata = $.map(data, func =(obj) ->
        obj.id = obj.id;
        obj.text = obj.name;
        return obj;
    );
    return { results: mapdata };

I am thinking of a lot of possibilities, but at the end I am not sure where the field-data comes from. 我正在考虑很多可能性,但是最后我不确定字段数据来自何处。 It is inside the object, but it isn't written to the resulting HTML in any way. 它在对象内部,但不以任何方式写入到生成的HTML中。 And even if the id would be written as a selected option, the select2 script would need to know how to transform that into the string to show the real data. 即使将id写入选定选项,select2脚本也需要知道如何将其转换为字符串以显示真实数据。

Any idea how to achieve that the data is still written into a field after a failing validation? 知道如何通过失败的验证将数据仍然写入字段吗?

After trying out some things I found out how to do it. 在尝试了一些事情之后,我发现了如何做。 At first I just changed the empty array to be the :field variable,too. 起初,我也只是将空数组更改为:field变量。 This doesn't work too well because it only remembers the ID of the value that has been entered before and like this the SELECT2-script could not find the value to that key and nothing is shown. 这不太好用,因为它只记住以前输入的值的ID,这样SELECT2-脚本找不到该键的值,并且什么也没有显示。

Then I created a new variable inside the controller in which I place the array with name and id: 然后,我在控制器内部创建了一个新变量,在其中放置了具有名称和ID的数组:

field_object = ObjectsModel.find(@form_request.field.to_i)
@form_field = []
if !field_object.nil?
    @form_field =  [[field_object.name, field_object.id]]
end

And in the view I now use this field to show the available options: 在视图中,我现在使用此字段显示可用选项:

<%= bootstrap_form_for(@form_request, url: '/form/request_form') do |f| %>
    <%= f.select :field, @form_field, {label: 'Field label'} %>
    <%= f.submit "Submit form" %>
<% end %>

This works perfectly fine for me without the need to touch the SELECT2-script. 这对我来说非常好用,而无需触摸SELECT2脚本。 The possible values are still fetched by AJAX but already filled out fields will persist upon redirect to another action. 可能的值仍由AJAX提取,但已经填写的字段将在重定向到另一个操作时保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM