此代码使用表单构建器
成功上传和映像作为通知的一部分<%= form_for(@notice, html: { multipart: true }) do |f| %>
<%= hidden_field_tag :callsign, @notice.character.callsign %>
<%= f.hidden_field :commentee_id, value: nil %>
<%= f.hidden_field :latitude, id: "notice_latitude" %>
<%= f.hidden_field :longitude, id: "notice_longitude" %>
<div class="field">
<%= f.text_area :content, id: "dropfield", placeholder: "What's going on here?" %>
</div>
<span class="picture">
<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
</span>
<%= f.submit "Drop", class: "btn btn-success", onclick: "return validateDropForm();" %>
<button type="button" class="btn btn-danger" id="cancel_drop">Cancel</button>
<% end %>
但使用file_field_tag
失败:
<%= form_tag( {controller: "notices", action: "create"}, method: "post", class: "comment_form", html: { multipart: true } ) do %>
<%= hidden_field_tag :callsign, @character.callsign %>
<%= hidden_field_tag "notice[supernotice][commentee_id]", notice.id %>
<%= hidden_field_tag "notice[latitude]", notice.latitude, id: "comment_notice_latitude" %>
<%= hidden_field_tag "notice[longitude]", notice.longitude, id: "comment_notice_longitude" %>
<%= text_area_tag "notice[content]", '', rows: 1, id: "commentField-#{notice.id}", class: "comment_area" %>
<%= button_tag( type: 'submit', name: nil, title: 'Post',
class: 'btn btn-default btn-xs comment_submit',
onclick: "return validateCommentForm('#commentField-#{notice.id}');" ) do %>
<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
<% end %>
<%= file_field_tag "notice[picture]", accept: 'image/jpeg,image/gif,image/png',
class: "file_field", title: "Upload picture" %>
<%= button_to "Upload file", class: 'btn btn-default btn-xs uploadbutton' do %>
<span class="glyphicon glyphicon-upload" aria-hidden="true"></span>
<% end %>
<% end %>
file_field_tag代码成功打开“打开文件”窗口,以便您可以选择要上载的文件,并在提交后成功创建通知,:内容已存在,但不包含图片。日志显示,虽然图片最初包含在请求中,但在INSERT INTO数据库时它以某种方式变为零:
Started POST "/notices" for ::1 at 2015-07-31 22:21:57 +0100
Processing by NoticesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SnKZ...lg==",
"callsign"=>"bazzer",
"notice"=>{"supernotice"=>{"commentee_id"=>"15022"},
"latitude"=>"54.0239066230473",
"longitude"=>"-1.02996826171875",
"content"=>"This isn't going to work!",
"picture"=>"pic1.jpeg"}}
.
.
SQL (5.9ms) INSERT INTO "notices" ("content", "picture", "latitude", "longitude", "character_id", "created_at", "updated_at")
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING "id" [["content", "This isn't going to work!"],
["picture", nil], ["latitude", 54.0239066230473],
["longitude", -1.02996826171875],
["character_id", 1],
["created_at", "2015-07-31 21:21:57.451695"],
["updated_at", "2015-07-31 21:21:57.451695"]]
使用file_field而不是file_field_tag没有任何区别。
代码有什么问题?如何让file_field_tag成功上传图片?
修改
此代码:
<%= form_tag( {controller: "notices", action: "create"}, method: "post", class: "comment_form", multipart: true ) do %>
<%= hidden_field_tag :callsign, @character.callsign %>
<%= hidden_field_tag "notice[supernotice][commentee_id]", notice.id %>
.
.
...生成此HTML:
<input type="file" name="notice[picture]
[{:accept=>"image/jpeg,image/gif,image/png", :class=>"file_field",
:title=>"Upload picture"}]"
id="notice_picture_{:accept=>"image/jpeg,image/gif,image/png",
:class=>"file_field", :title=>"Upload picture"}"/>
答案 0 :(得分:1)
我认为你不想要html:
密钥
<%= form_tag( {controller: "notices", action: "create"}, method: "post", class: "comment_form", multipart: true ) do %>
:multipart是一个选项。