19

I am trying to add an Avatar Upload field to my Profile Page, but as soon as I add the :html => {:multipart => true} to it, it spits out an syntax error.

<%= form_for(@user), :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :email %>
<%= f.email_field :email %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>


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

    <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>

The error is:

syntax error, unexpected tASSOC, expecting keyword_end
...end=  form_for(@user), :html => { :multipart => true } do |f...
...                               ^
Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
Martin Lang
  • 355
  • 1
  • 3
  • 11

3 Answers3

35

It should be like this:

form_for @user, :html => { :multipart => true } do |f|

The parenthesis in form_for(@user) is actually telling Ruby interpreter the function is invoked with only one parameter, and you can't pass wrong number of arguments in a method in Ruby.

Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
Alston
  • 1,096
  • 1
  • 12
  • 25
30

http://guides.rubyonrails.org/form_helpers.html#uploading-files

It's either a form_tag helper with multipart => true or just form_for

<%= form_tag({:action => :upload}, :multipart => true) do %>
  <%= file_field_tag 'picture' %>
<% end %>

<%= form_for @person do |f| %>
  <%= f.file_field :picture %>
<% end %>
Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
Benjamin Udink ten Cate
  • 12,052
  • 3
  • 43
  • 63
1

I'm assuming that you're either using 'paperclip' or ''carrierwave' to upload images. So, try this:

<%= form_for @user, : url => users_path, :html => {:multipart => true} do |f| %>
Adam
  • 11
  • 1
  • 4