1

我正在尝试将我的网页转换为新的 Twitter Bootstrap HAML 方法,但是在将图像上传到表单时遇到了麻烦。该表单是 POST 到使用 activerecord 将参数放入数据库的 API。在旧网页上提交应用程序有效,并且通过 API 有效,而不是使用我的新 HAML 表单。下面是图标上传字段的新旧代码,以及图片上传到数据库的API代码。

我不断收到的错误是“无法将符号转换为整数”,我认为问题在于从实际上传的图标中获取 [:filename]。

新代码:

    %div{:class=>classes_for_form_input(@data, :icon)}
      %label.control-label{:for => "icon"} *Icon:
        .controls
          %input{:name=>"icon", :type=>"file", :id => "icon", :class => "fileinput", :title => "App Icon", :value=>@data[:icon]}
          %span.help-inline #{help_message_for_form_input @data, :icon }

旧代码:

<td class="uploadtabletd1l"><label for="icon">*Icon: </label></td><td class="uploadtabletd1r"><input type="file" class="fileinput" name="icon" id="icon" size="18" title="Icon (128x128 Pixels, Max 100 Characters)" />#{tip1}#{iconhelp}#{tip2}</td>

上传图标的 API 循环:

attr = app.attributes()
attr.each do |k,v|

  if @vals.include?(k)
    if k == "icon" then  
      attr[k] = @vals[k][:filename]
    else  
      attr[k] = @vals[k] 
    end  
  end
end

堆栈错误消息给出以下错误,因为我正在使用会话 :success_message 重定向到显示已上传应用程序的页面。

 NoMethodError at /appdetails
 undefined method `first' for nil:NilClass
 file: home.rb location: block in <top (required)> line: 94
/gas/dev/routes/home.rb in block in <top (required)>
 @app = result['appinfo'].first
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb in service
  si.service(req, res)
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb in run
      server.service(req, res)
/usr/local/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
      block ? block.call(sock) : run(sock)

“无法将符号转换为整数来自以下代码行,我尝试打印 api 调用的结果以在数据库中创建应用程序

result = @client.createapp(@vals) # This is calling the API to return the vals that were inputted into the database
puts result.to_s

这是 home.rb 文件:第 94 行是 @app = result['appinfo'].first

get '/appdetails/?' do
# include some page specific scripts and styles via the layout
@page_scripts = ["jquery.colorbox-min.js", "gass-apps.js", "jquery.rateit.min.js"]
@page_styles = ["colorbox.css", "rateit.css"]

#TODO Handle case for unknown app id
result = @client.appinfo(:app_id=>params[:id])
puts __FILE__.to_s + '(' + __LINE__.to_s + '):' + result
@app = result['appinfo'].first

#get screenshots into an array that the view can iterate through
screenshot_properties = ["screenshot1", "screenshot2", "screenshot3", "screenshot4"]
@screenshots = []

screenshot_properties.each do |screenshot|
  @screenshots << @app[screenshot] if @app.has_key? screenshot
end

@reviews = @client.getreviews(:app_id=>params[:id], :order=>'reverse')['getreview']

@favorite = false
#TODO - a more direct API call would be better than iterating over all the favorites
userFavorites = @client.getuserfavorites()['getuserfavorites']
userFavorites.to_a.each do |favorite|
  @favorite = true if favorite['app_id'] == params[:id].to_i
end

# Increment the view count for this app
plural = "s"
viewcount = @app['view_count']
if viewcount.to_s == '0'
  plural = ""
end
viewcount += 1
result = @client.updateappviewcount(:app_id=>params[:id], :view_count=>viewcount)

OK:一般来说是愚蠢的错误。我复制粘贴了表单集并忘记包含 enctype="multipart/form-data" 。但是,Sinatra 现在返回 enctype 未定义?我需要这个吗?

4

1 回答 1

2

问题在于表单标签。忘记包含 "enctype="multipart/form-data" 或 HAML :enctype="multipart/form-data"

于 2012-10-04T14:37:58.430 回答