ruby-on-rails Rails:在更改时提交表单

tcomlyy6  于 2023-08-08  发布在  Ruby
关注(0)|答案(4)|浏览(107)

我怎么才能让这个工作起来?

<%= 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 %>

字符串
目标是在更改时自动提交表单。但是上面的代码不起作用。出现wrong number of arguments (5 for 1..4)错误。有什么想法吗?

7bsow1i6

7bsow1i61#

最后三个参数实际上是选项的散列。如果你喜欢,试着把它们放在花括号里,让它更清楚:

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

字符串

z4bn682m

z4bn682m2#

找到了,去掉{prompt:true}就可以了!

ukqbszuj

ukqbszuj3#

选择助手:

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 select helper

whlutmcx

whlutmcx4#

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()"} %>

相关问题