嵌套属性。字段不更新

时间:2016-02-16 18:52:12

标签: ruby-on-rails ruby attributes nested

我有这样的模型:

class ListiclesController < ApplicationController

def basic_listicle_params
    params.require(:listicle).permit :item_separator_color, :vote_count_color, :arrows_default_color,
                                     :show_shareaholic, :arrows_on_hover_color, :arrows_selected_color, :items_order,
                                     :show_stickers, :intro, :footer, :show_vote_feedback, :stickers_for_each_item,
                                     on_load_scripts_attributes: [:id, :script, :position, :_destroy],
                                     questions_attributes: [:body, :_destroy, :id],
                                     listicle_sticker_tags_attributes: [:id, :listicle_sticker_id, :text, :_destroy]

....
end

控制器有:

.form-group
  -@listicle.ordered_questions.each do |question,idx|
    =form.label 'ID:'
    = question.id
    =form.label 'Title:'
    = question.body.present? ? question.title : "Item #{idx + 1}"
    - @stickers.each do |sticker|
      = sticker.id
      = sticker.text


      = form.fields_for :listicle_sticker_tags do |u|
        .nested-fields.form-group
          .field

            = link_to_remove_association u, class: %(text-danger pull-right) do
              %i.glyphicon.glyphicon-remove-circle{style:"position: relative; top: 25px; right: 2px; font-size: 18px;"}
            = u.text_area :text, rows:4, class: %w(form-control)
            = u.hidden_field :listicle_sticker_id, :value => sticker.id
      .links
        %br
        =link_to_add_association 'Add script', form, :listicle_sticker_tags, class: %w(btn btn-primary pull-right)

观点:

 SQL (0.6ms)  INSERT INTO `listicle_sticker_tags` (`listicle_id`, `text`) VALUES (39, 'hhhhhhhhhhhhhh')
   (4.7ms)  COMMIT
  Rendered listicles/update.js.erb (0.4ms)

问题是:listicle_sticker_id不会更新,并以nil的形式返回。 如你所见,它甚至没有更新:

2.1.1 :067 > ListicleStickerTag.where(listicle_id: 39)
  ListicleStickerTag Load (0.9ms)  SELECT `listicle_sticker_tags`.* FROM `listicle_sticker_tags`  WHERE `listicle_sticker_tags`.`listicle_id` = 39
 => #<ActiveRecord::Relation [#<ListicleStickerTag id: 35, listicle_id: 39, text: "hhhhhhhhhhhhhh", listicle_sticker_id: nil>]>

控制台输出如下:

val request: WSRequest = WS.client
   .url(s"https://www.instagram.com/explore/tags/gibtsnicht")
   .withRequestTimeout(40000)

我缺少什么才能使listicle_sticker_id在提交时更新?

1 个答案:

答案 0 :(得分:0)

修改强params后重新启动服务器对我来说很有帮助。