【问题标题】:Rails file field doesn't change the name when using multipleRails 文件字段在使用多个时不会更改名称
【发布时间】:2013-11-17 07:04:01
【问题描述】:

当我的文件字段使用多个时,它会显示一个名称,并在末尾附加一个数组 [],但我正试图摆脱它。我没有得到我应该得到的正确名字。我应该通过硬编码从名称中删除[],名称仍然给我business_photos[bizurl][]

<%= form_for BusinessPhoto.new, :html => {:class => "biz_image"} do |f| %>
    <%= f.file_field :bizurl, multiple: true, name: "business_photos[bizurl]" %>
    <%= f.hidden_field :business_id, :value => @biz.id %>
 <%= f.submit %>
<% end %>

我有什么遗漏吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 file-upload nested-form-for


    【解决方案1】:

    使用 file_field 会自动在名称中添加 [] 数组。

    如果你想删除它,你可以使用file_field_tag 而不是f.file_field

    f.file_field :foo, multiple: true, name: 'foo' # results in "name='foo[]'" (f is a form-helper)
    file_field_tag :foo, multiple: true, name: 'foo' # results in "name='foo'"
    

    类似discussion here

    【讨论】:

      【解决方案2】:

      你没有传递 html 选项

      :multipart => true
      

      使用 form_for

      【讨论】:

      • 我以前有过,但我的名字末尾还有[]
      • 是的,我已经重启了服务器
      • 3.2 API看来,multipart已经从file_field方法self.multipart = true设置好了
      猜你喜欢
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多