我拼命想在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">
请帮忙!
答案 0 :(得分:1)
当您添加f.file_field
来电时,它应自行更改为多部分:
在form_for块中使用此方法会将封闭表单的编码设置为multipart / form-data。
正如在评论中发现的那样,错误是由于在部分(而不是局部变量)中使用@listing
。
如果您有_listing_form.html.erb
部分,则应手动pass the local variables:
<%= render partial: 'shared/listing_form', locals: {listing: @listing} %>