1

Web ページを新しい Twitter Bootstrap HAML アプローチに変換しようとしていますが、画像をフォームにアップロードするときに問題が発生しています。このフォームは、activerecord を使用してパラメーターをデータベースに入れる API への POST です。古い W​​eb ページでアプリを送信すると機能し、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" を含めるのを忘れていました。しかし、シナトラは enctype が定義されていないことを返すようになりましたか? これを何らかの形で要求する必要がありますか?

4

1 に答える 1

2

問題はフォームタグにありました。"enctype="multipart/form-data" または HAML :enctype="multipart/form-data" を含めるのを忘れました

于 2012-10-04T14:37:58.430 に答える