Ruby-on-rails Form_for with :multipart => true 吐出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10526641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:22:44  来源:igfitidea点击:

Form_for with :multipart => true spits out

ruby-on-railsform-for

提问by Martin Lang

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.

我正在尝试将 Avatar Upload 字段添加到我的个人资料页面,但是一旦我添加: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 %>

The error is:

错误是:

syntax error, unexpected tASSOC, expecting keyword_end
...end=  form_for(@user), :html => { :multipart => true } do |f...
...                               ^

回答by Alston

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.

中的括号form_for(@user)实际上是在告诉 Ruby 解释器该函数仅使用一个参数调用,并且您不能在 Ruby 中的方法中传递错误数量的参数。

回答by Benjamin Udink ten Cate

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

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

It's either a form_taghelper with multipart => trueor just form_for

它要么是一个form_tag帮手,multipart => true要么只是form_for

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

<%= form_for @person do |f| %>
  <%= f.file_field :picture %>
<% end %>

回答by Adam

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

我假设您使用“回形针”或“carrierwave”上传图像。所以,试试这个:

<%= form_for @user, : url => users_path, :html => {:multipart => true} do |f| %>