我試圖將我的網頁轉換爲新的Twitter Bootstrap HAML方法,但是當將圖像上傳到表單時遇到了麻煩。表單是POST到使用activerecord將參數放入數據庫的API。在舊網頁上提交應用程序,並通過API工作,不與我的新HAML表單一起提交。以下是圖標上傳字段的新舊代碼,以及將圖像上傳到數據庫的API代碼。使用activerecord將圖像上傳到Postgres數據庫時出現錯誤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 =結果[「APPINFO」]第一
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)
行:。在一般啞錯誤,我複製粘貼的形式設置在忘了包括ENCTYPE =「多部分/ form-data「。但是, Sinatra現在正在返回enctype未定義?我需要這個嗎?
另一件需要注意的事情是我運行了」puts「命令,並將@vals [圖標「]在API循環到達圖標之前返回圖像名稱,但在到達圖標循環後失敗 – cjl5108
錯誤引用了您尚未提供的文件。 – willglynn