在我的表单中,当复选框被选中时,我想动态添加一个字段。
.select-qty有css dislpay: none
<%= form_with model: @item, html: { class: "update-form", method: :patch, remote: true } do |f| %>
<%= f.hidden_field :id, value: item.id %>
<td class="mulitple-item">
<%= f.check_box :selected, { checked: true, value: item.selected, class: 'form-control', onchange: 'this.form.submit();' } %>
</td>
<td class="select-qty>
<%= f.select :quantity, options_for_select((0..item.quantity), selected: 2), {}, { value: item.quantity, class: 'form-control', onchange: 'this.form.submit();' } %>
</td>
<% end %>我有这个update.js.erb,它可以让这个字段出现...
$('.multiple-item').bind('.update-form').bind('.submit-btn').bind('ajax:success', function(){
$(".select-qty").css("display", "block")
});在我的路线中,我有:
patch "items", to: "items#update", defaults: { format: 'js' }和我的控制器#update
def update
@item = Item.find(params[:id])
@item.update_attributes(item_params)
respond_to do |format|
format.js
end
end当我选中复选框时,它会在浏览器中呈现update.js.erb的原始代码...
我做错了什么?
发布于 2019-04-09 06:54:51
method和remote不应该在html: {}散列中,因为它们实际上不是html属性,而是form_with帮助器方法的选项。
实际上,html表单具有method属性,但是该选项通常传递给html散列之外的form_with方法,因为Rails不仅将其作为属性包含在生成的html表单中,还会执行其他操作。
这是正确的方式:
<%= form_with model: @item, method: :patch, remote: true, html: {class: "update-form"} do |f| %>发布于 2019-04-09 15:40:38
我最终删除了我的路由中的defaults {format: :js}
patch "items", to: "items#update"我在我的update方法中添加了html格式...
def update
@item = Item.find(params[:id])
@item.update_attributes(item_params)
respond_to do |format|
format.js
format.html {redirect_to my_path}
end
endhttps://stackoverflow.com/questions/55580461
复制相似问题