【问题标题】:The current value is not saved after a re edition重新编辑后不保存当前值
【发布时间】:2016-10-16 07:46:00
【问题描述】:

我的重新编辑有问题。让我解释一下:当我编辑和更新对象时,当前值在我的显示视图中是可以的。但是如果我重新编辑当前值不是最后选择的。

问题仅在于复选框或选择。

我的表格

<%= f.fields_for :situations do |s| %>

<p><label for="plage">Plage</label>
<%= s.select :plage?, ["", "oui","non"] %> à <%= s.select :distanceplage?, ["", "moins de 1", "2","3", "4", "5 et plus"] %> km</p>
<% end %>

控制器

def edit

end

 def update
      @camping = Camping.find(params[:id])
      respond_to do |format|
        if @camping.update(camping_params)
          format.html { redirect_to @camping, notice: 'Camping was successfully updated.' }
          format.json { render :show, status: :ok, location: @camping }
        else
          format.html { render :edit }
          format.json { render json: @camping.errors, status: :unprocessable_entity }
        end
      end
    end

我该如何解决?感谢您的帮助!

【问题讨论】:

  • 我认为数据库中的当前值是好的。你检查过吗?只是您没有使用数据库值预加载选择器。您改为创建一个新的空白标签...
  • 是的,当前值很好。问题仅在显示选择和复选框上。如果我添加 text_field 值就可以了...我不知道为什么...
  • 抱歉,我不在电脑前,但我想我设法通过 selector_tag 和 option_for_select 选项做到了这一点。有人可能会发布完整的答案。如果没有,我明天会回来告诉你我的解决方案。
  • @Maxence 谢谢,我希望有人可以帮助我。
  • Selector_tag 和 check_box_tag 不好玩。每次修改这些标签时都要检查生成的 html。好勇气

标签: ruby-on-rails checkbox edit params


【解决方案1】:

好的,经过一些搜索解决方案是这样的:

:include_blank => true

所以我的观点是这样的

<p><label for="plage">Plage</label>
<%= s.select :plage, ["oui","non"], :include_blank => true %> à <%= s.text_field :distanceplage %> km</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2017-11-02
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多