javascript - select2 不保存编辑输入

标签 javascript ruby-on-rails ruby jquery-select2

由于某种原因,当用户去编辑条目时,他通过 .select2 输入的数据都没有显示。虽然数据已保存。

irb(main):001:0> Routine.find(1)
=> #<Routine id: 1, missed_days: 0, date_started: "2015-10-24 04:00:00", trigger: "brew tea", action: "stretch", user_id: 1, created_at: "2015-11-24 21:55:25", updated_at: "2015-12-14 21:00:09", committed: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""], days_challenged: 30>

show.html.erb

enter image description here

edit.html.erb

enter image description here

_form.html.erb

<%= simple_form_for(@routine, remote: request.xhr?, html: { data: { modal: true } }) do |f| %> 
  <%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
  <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
  <%= f.number_field :days_challenged, value: 30, class: 'day-challenge' %> <b>Day Challenge
  <label>After I</label> 
    <%= f.grouped_collection_select(:trigger, @trigger, :last, :first, :to_s, :to_s, include_blank: true) %><font color="#DDD">,</font>
  <label>I will</label> 
    <%= f.grouped_collection_select(:action, @action, :last, :first, :to_s, :to_s, include_blank: true) %>.
  <%= button_tag(type: 'submit', class: "btn")  do %>
   Save
  <% end %>
<% end %>

<script>
  $("#routine_trigger").select2({
    placeholder: "Existing Habit (Optional)",
    theme: "classic",
    allowClear: false,
    tags: true,
    multiple: false,
  });
  $("#routine_action").select2({
    placeholder: "Enter Challenge",
    allowClear: false,
    tags: true,
    multiple: false,
  });
</script>

routines_controller

  def new
    @trigger = 
    [
    ['Morning', ['Get Out of Bed', 'Clean Up Breakfast', 'Brush My Teeth', 'Sit at Desk', 'Start My Computer']], 
    ['Afternoon', ['Drink Coffee', 'Read Email', 'Eat Lunch', 'Use the Bathroom', 'Go for a Walk']],
    ['Evening', ['Enter My Home', 'Eat a Snack', 'Make a Drink', 'Shower', 'Finish Dinner']]
    ]
    @action = 
    [
    ['Mind', ['Write 500 Words', 'Read a Chapter', 'Study 20 min', 'Watch a Ted Talk', 'Take a Picture']], 
    ['Body', ['Do 25 Pushups', 'Juice Cleanse', 'Walk 10,000 Steps', 'Exercise', 'Eat an Apple']],
    ['Spirit', ['Meditate', 'Write 3 Gratitudes', 'Journal', 'Not Complain', 'Do a Random Act of Kindness']]
    ]
    if current_user == nil
      @routine = Routine.new
    else
      @routine = current_user.routines.build
      respond_modal_with @routine
    end
  end

  def edit
    @trigger = 
    [
    ['Morning', ['Get Out of Bed', 'Clean Up Breakfast', 'Brush My Teeth', 'Sit at Desk', 'Start My Computer']], 
    ['Afternoon', ['Drink Coffee', 'Read Email', 'Eat Lunch', 'Use the Bathroom', 'Go for a Walk']],
    ['Evening', ['Enter My Home', 'Eat a Snack', 'Make a Drink', 'Shower', 'Finish Dinner']]
    ]
    @action = 
    [
    ['Mind', ['Write 500 Words', 'Read a Chapter', 'Study 20 min', 'Watch a Ted Talk', 'Take a Picture']], 
    ['Body', ['Do 25 Pushups', 'Juice Cleanse', 'Walk 10,000 Steps', 'Exercise', 'Eat an Apple']],
    ['Spirit', ['Meditate', 'Write 3 Gratitudes', 'Journal', 'Not Complain', 'Do a Random Act of Kindness']]
    ]
    respond_modal_with @routine
  end

最佳答案

这就是我在 select2 字段上所做的

<%= f.input :field_name, as: :select, collection: f.object.field_name, include_blank: false, selected: f.object.field_name, input_html: { class: "json_data" }, %>

json_data 是我将文本字段变成 select 2 的方式

咖啡

$('.json_datas').before ()->
    "<input name='#{@.name}' type='hidden' />"
  $('.json_data').select2
    allowClear: true,
    placeholder: "Select a value",
    ajax:
      url: '/api/call_to_data'
      dataType: 'json'
      delay: 250
      data: (params) ->
        {
          q: params.term
          page: params.page
        }
      processResults: (data, page) ->
        # parse the results into the format expected by Select2.
        # since we are using custom formatting functions we do not need to
        # alter the remote JSON data
        { results: data.items }
      cache: true

希望对你有帮助

关于javascript - select2 不保存编辑输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298444/

相关文章:

javascript - 使用 javascript 更新文本框

javascript - 为什么我们需要使用 flatMap?

javascript - 隐藏未选中的单选按钮

ruby-on-rails - 不使用区 block 回滚交易

ruby-on-rails - FactoryGirl 记录未保存

ruby - 带有 rvm 的 ubuntu 12.04 上的 ruby​​ 缺少一些 readline 库

javascript - AngularJS $http 返回值

ruby-on-rails - 讨论 bluepill 使用 rbenv

ruby - 如何卸载 RubyMine?

ios - 不同 ios 中的 Rubymotion 应用程序