Ruby on rails 如何通过ajax将BLOBURL传递给rails创建方法

Ruby on rails 如何通过ajax将BLOBURL传递给rails创建方法,ruby-on-rails,ruby,ajax,Ruby On Rails,Ruby,Ajax,我想在Rails项目中上传声音。 下载录音效果很好,但我无法将数据发送到rails服务器 recorder && recorder.exportWAV(function (blob) { var url = URL.createObjectURL(blob); console.log(url); $.ajax({ type: "POST", url: "/voices",

我想在Rails项目中上传声音。 下载录音效果很好,但我无法将数据发送到rails服务器

recorder && recorder.exportWAV(function (blob) {
        var url = URL.createObjectURL(blob);
        console.log(url);

        $.ajax({
            type: "POST",
            url: "/voices",
            data: {voice: {sound: url}}
        });
    });
服务器日志中有post数据,但未创建
声音

Started POST "/voices" for ::1 at 2015-12-08 20:43:16 +0900
Processing by VoicesController#create as */*
  Parameters: {"voice"=>{"sound"=>"blob:http%3A//localhost%3A3000/3ad3859e-b960-44b8-ba18-20727e739bab"}}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Profile Load (0.3ms)  SELECT  "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 LIMIT 1  [["user_id", 1]]
   (0.2ms)  BEGIN
  SQL (0.5ms)  INSERT INTO "voices" ("sound", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["sound", nil], ["created_at", "2015-12-08 11:43:16.254713"], ["updated_at", "2015-12-08 11:43:16.254713"]]
   (0.6ms)  COMMIT
Completed 500 Internal Server Error in 135ms (ActiveRecord: 3.4ms)
如何将声音blob数据传递到rails服务器

语音控制器.rb
如果您试图正确发布数据或正确设置模型,则此代码会导致错误
未捕获类型错误:非法调用

不确定。您需要使用file.open(filename,w)将音频另存为文件

SQL表明,没有任何东西被传递到“声场”

  • 如果要将其作为blob传递,则需要相应地设置列(如果不完全设置,则更好的选项是2)
  • 将音频数据发送并保存为文件。看看


  • 您的控制器代码不可用,查看日志我猜在保存模型时,您没有传递要保存的声音。
    在135ms(ActiveRecord:3.4ms)内完成了500个内部服务器错误。
    您可以附加错误消息吗?我添加了控制器。我是否保存错误?@richfisher这是因为我忘记添加相应的视图模板,还有
    500个内部服务器错误
    。很抱歉,我没有写足够的信息。我在用载波处理文件。我仍然不知道如何处理二进制文件。如果你能帮助我,我非常感谢。看一下更新的答案,你正在发送一个blob url,它只存在于你的浏览器中,更好的方法应该是将实际的blob数据发送到你的rails应用程序。我更改了行,然后我得到了
    未捕获类型错误:非法调用
    错误。我被卡住了..我想,这是jQuery在处理blob数据时错误地设置头的错误,你能看一下和吗。您可能需要将
    processData:false
    添加到ajax调用中。最后!我只需要添加
    processData:false
    contentType:false
    ,就像你给我看的帖子一样。谢谢
    class VoicesController < ApplicationController
        @voice = Voice.new(voice_params)
    
        respond_to do |format|
          if @voice.save
            format.html { redirect_to root_path, notice: 'voice was successfully created.' }
            format.json { render :show, status: :created, location: root_path }
          else
            format.html { render :new }
            format.json { render json: @voice.errors, status: :unprocessable_entity }
          end
        end
    
      def voice_params
        params.require(:voice).permit(:sound, :user_id)
      end
    end
    
    class Voice < ActiveRecord::Base
      belongs_to :phrase
      belongs_to :user
      mount_uploader :sound, SoundUploader
    end
    
    recorder && recorder.exportWAV(function (blob) {
        var url = URL.createObjectURL(blob);
        console.log(url);
    
        var formData = new FormData();
        formData.append('voice[sound]', url);
    
        $.ajax({
            type: "POST",
            url: "/voices",
            data: formData
        });
    });
    
        SQL (0.5ms)  INSERT INTO "voices" ("sound", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["sound", nil], ["created_at", "2015-12-08 11:43:16.254713"], ["updated_at", "2015-12-08 11:43:16.254713"]]
    
    formData.append('voice[sound]', url); # Comment this
    formData.append('voice[sound]', blob); # This will send the actual data to your server and not the blob url. then your carrierwave will have the file data, which it can save.