Rails: 在表单更改时提交表单

5

我该如何让它工作?

<%= form_for current_user, html: {multipart: true } do |f| %>
  <%= f.select(:brand, Brand.pluck(:company), {prompt:true}, {class: 'form-control'}, {:onchange => 'this.form.submit()'}) %>
<% end %>

目标是在表单更改时自动提交表单。但上面的代码不起作用。我收到了一个参数数量错误(5个而不是1..4)的错误。有什么想法吗?

4个回答

8

最后三个参数实际上是一个选项哈希。如果你愿意的话,可以将它们放入花括号中以使其更加清晰:

<%= f.select(:brand, Brand.pluck(:company), {
  prompt: true,
  class: 'form-control',
  onchange: 'this.form.submit()'
}) %>

对于我而言,我必须将花括号移到这个位置:<%= f.select(:brand, Brand.pluck(:company), {}, { class: 'form-select', onchange: 'this.form.submit()' }) %>。 - don_Bigote

0

选择助手:

select(object, method, choices = nil, options = {}, html_options = {}, &block)

这也应该适用于prompt:

<%= f.select(:brand, Brand.pluck(:company), {include_blank: true, class: 'form-control'}, :onchange => 'this.form.submit()') %>

Rails 选择助手


0

明白了。移除 {prompt:true} 就可以了!


0
在Rails 7中
    <%= f.select :city, ["Lahore", "Chicago", "Madrid"] , {include_blank: "none"} ,onchange: "this.form.requestSubmit()" %>

使用集合选择
    <%= f.collection_select :user_id, User.all,:id,:name,{include_blank: "none"} , {onchange: "this.form.requestSubmit()"} %>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接