我试图添加一个头像上传字段到我的个人资料页面,但一旦我添加:html => {:multipart => true},它会吐出语法错误.
<%= 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 %>
错误是:
Syntax error,unexpected tASSOC,expecting keyword_end ...end= form_for(@user),:html => { :multipart => true } do |f... ... ^
解决方法
http://guides.rubyonrails.org/form_helpers.html#uploading-files
它是一个form_tag帮助器,multipart =>真或只是form_for
<%= form_tag({:action => :upload},:multipart => true) do %> <%= file_field_tag 'picture' %> <% end %> <%= form_for @person do |f| %> <%= f.file_field :picture %> <% end %>