Bazley
Bazley

Reputation: 2847

Rails form_for uploads picture but file_field_tag does not

This code successfully uploads and image as part of a Notice, using a form builder:

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

But using a file_field_tag fails:

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

The file_field_tag code successfully opens an "open file" window so you can select the file you want to upload, and upon submission the notice is successfully created, the :content is present, but the picture is not included. The logs show that while the picture is initially included in the request, it somehow has become nil upon INSERT INTO the database:

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"]]

Using file_field instead of file_field_tag makes no difference.

What is wrong with the code? How do I get the file_field_tag to successfully upload a picture?

EDIT

This code:

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

...produces this 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"}"/>

Upvotes: 0

Views: 1362

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18100

I don't think you want the html: key

<%= form_tag( {controller: "notices", action: "create"}, method: "post", class: "comment_form", multipart: true ) do %>

:multipart is an option.

Upvotes: 1

Related Questions