开发者

Rails multipart Post Problem

开发者 https://www.devze.com 2023-01-05 05:56 出处:网络
I have the below code in my controller <% form_for @picture, :picture, :url=>pictures_url, :html => { :multipart => true } do | form | %>

I have the below code in my controller

<% form_for @picture, :picture, :url=>pictures_url, :html => { :multipart => true } do | form | %>
  <%=form.file_field :image%>
  <%=submit_tag 'Save'%>
<%end%>

which submits to the below controller

class PicturesController < ApplicationController
  def create
    render :text=>1
  end
end

When I turn on the :multipart=>true I get the following error however if I dont use the multipart then it all performs normally?

/!\ FAILSAFE /!\  Tue Jun 29 09:59:47 +0100 2010
  Status: 500 Internal Server Error
  wrong number of arguments (1 for 0)
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:338:in `initi
ze'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:338:in `new'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:338:in `parse
ltipart'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:323:in `loop'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/utils.rb:323:in `parse
ltipart'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/request.rb:133:in `POS
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:15:i
call'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/par
_parser.rb:15:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/ses
n/cookie_store.rb:93:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/fai
fe.rb:26:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchro
e'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dis
cher.rb:114:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/rel
er.rb:34:in `run'
    C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dis
cher.rb:108:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in
all'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:1
n `call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:i
call'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:
`service'
    C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    C:/Ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
    C:/Ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
    C:/Ruby/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:
`run'
    C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
    C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/cust开发者_如何学编程om_require.rb:31:in `gem_orig
l_require'
    C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

Any suggestions, sorry if this is a newbie mistake but I'm just starting out learning rails.

Thanks

Andy


Ref examples given here

<% form_for :picture,  @picture, :url=>pictures_url, :html => { :multipart => true } do | form | %>
  <%=form.file_field :image%>
  <%=submit_tag 'Save'%>
<%end%>

Edit

try to give pictures_url as following

<% form_for(@picture, :url => {:controller => "pictures", :action => "create"}, 
                      :html => {:multipart => true }) do |form| %>


If you're wrapping your form_for around an object (in your case, @picture), you don't need to explicitly specify the model (:picture) or the :url.

Maybe the following will do the trick

<% form_for @picture, :html => { :multipart => true } do |f| %>
  ...
<% end %>


Try:

<% form_for @picture, :html => { :multipart => true } do | form | %>
  <%=form.file_field :image%>
  <%=submit_tag 'Save'%>
<%end%>

The API holds great examples for such problems ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号