Ruby form_for html 属性无效,multipart 或 id

Ruby form_for html attributes not working, multipart or id

我拼命想在 Ruby 中将多部分添加到我的表单中,但它不会显示。我到处都在网上查找,但无论我尝试什么,它都没有显示。即使是简单的 ID 或 类 也行不通...

有没有我不知道的依赖关系?

<%= form_for @listing, :html => {:id => "account_form", :multipart => true } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.label :title %>
      <%= f.text_field :title, class: 'form-control' %>

      <%= f.label :highlights %>
      <%= f.text_area :highlights, class: 'form-control' %>

      <%= f.label :location %>
      <%= f.text_area :location, class: 'form-control' %>

      <%= f.label :catering %>
      <%= f.text_area :catering, class: 'form-control' %>

      <%= f.label :travel %>
      <%= f.text_area :travel, class: 'form-control' %>

      <%= f.label :dates %>
      <%= f.text_area :dates, class: 'form-control' %>

      <%= f.label :price %>
      <%= f.text_field :price, class: 'form-control' %>

      <%= f.label :category %>
      <%= f.select :category, options_from_collection_for_select(Category.all, :id, :name), :include_blank => true %>

      <%= f.label :country %>
      <%= f.text_field :country, class: 'form-control' %>

      <%= f.label :url %>
      <%= f.text_field :url, class: 'form-control' %>

      <%= f.label :photo %>
      <%= f.file_field :photo %>

  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

这将导致以下结果 HTML

<form class="new_listing" id="new_listing" action="/listings" accept-charset="UTF-8" method="post">

请帮忙!

当你添加 f.file_field 调用时,它应该自己更改为 multipart:

Using this method inside a form_for block will set the enclosing form’s encoding to multipart/form-data.

正如在评论中发现的那样,错误是由于在局部变量(而不是局部变量)中使用了 @listing

如果您有 _listing_form.html.erb 部分,您应该 pass the local variables 手动:

<%= render partial: 'shared/listing_form', locals: {listing: @listing} %>