将form_tag变量传递给controller + API

时间:2014-05-27 16:56:27

标签: ruby-on-rails file-upload wistia

我已经为这个问题奋斗了2天,但无法克服以下步骤。

我正在尝试使用Wistia API上传视频,但是在将文件名和数据从视图传递到控制器的那一刻就陷入困境。

我正在使用官方http://wistia.com/doc/upload-api#examples_using_ruby API文档,但继续 "没有将符号隐式转换为字符串"错误。

我真的很感激任何帮助或提示。

我的观点index.html.erb:

<h1>File Upload</h1>
<%= form_tag('/uploads', method: :post, multipart: true) do %>

<%= file_field 'pic', 'data' %></p>
<%= submit_tag "Upload" %>
<%= debug params %>
<% end %>

我的控制器上传控制器:

class UploadsController < ApplicationController
  def home
  end

  def uploadFile
  end

  def create
    @xyz = post_video_to_wistia(:pic, :data)
  end


  require 'net/http'
  require 'net/http/post/multipart'

    def post_video_to_wistia(name, path_to_video)
  uri = URI('https://upload.wistia.com/')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  # Construct the request.
  request = Net::HTTP::Post::Multipart.new uri.request_uri, {
    'api_password' => '<API_PASSWORD>',
    'contact_id'   => '<CONTACT_ID>', # Optional.
    'project_id'   => '<PROJECT_ID>', # Optional.
    'name'         => '<MEDIA_NAME>', # Optional.

    'file' => UploadIO.new(
                File.open(path_to_video),
                'application/octet-stream',
                File.basename(path_to_video)
              )
  }

  # Make it so!
  response = http.request(request)

  return response
end

end

参数:

{"utf8"=>"✓",
 "authenticity_token"=>"/dRprM7ZjQzzb9N9OErk0Dy4tSt/+zsUldpOed8jCqY=",
 "pic"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x3ef23e0 @tempfile=#<Tempfile:C:/Users/Maciek/AppData/Local/Temp/RackMultipart20140527-7076-bo3tzy>,
 @original_filename="example.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"pic[data]\"; filename=\"example.jpg\"\r\nContent-Type: image/jpeg\r\n">},
 "commit"=>"Upload"}

1 个答案:

答案 0 :(得分:0)

更新create操作如下:

def create
  @xyz = post_video_to_wistia(:pic, params[:pic][:data]) ## Notice params[:pic][:data]
end

注意:

params[:pic][:data]包含通过表单输入和提交的path_to_video

目前,您正在调用post_video_to_wistia方法,其中第二个参数为:data(这是符号),即您在调用的方法中接收参数path_to_video = :data

您必须在线

收到错误
'file' => UploadIO.new(
            File.open(path_to_video), ## <== Problem here
            'application/octet-stream',
            File.basename(path_to_video)
          )

因为,File.open(path_to_video)传递的符号不是传递视频的实际路径,而是导致错误

no implicit conversion of Symbol into String

Ruby无法将:data转换为path_to_video(例如:/ path / to / myvideo)。

如上所述,您需要将params[:pic][:data]作为第二个参数传递给post_video_to_wistia方法才能使代码生效。

<强>更新

对于评论中的错误

  

我已经按照你的指导,但现在得到了#34;没有隐含的转换   ActionDispatch :: Http :: UploadedFile into String&#34;

您需要做的是,更新以下代码

'file' => UploadIO.new(
            File.open(path_to_video),
            'application/octet-stream',
            File.basename(path_to_video)
          )

。通过

'file' => UploadIO.new(
            path_to_video.tempfile,  ## <== Changed this
            'application/octet-stream',
            path_to_video.original_filename ## <== Changed this
          )

此更改是由于您已在path_to_videoActionDispatch::Http::UploadedFile类的实例)中收到文件,您可以通过path_to_video.tempfile访问该文件并获取通过path_to_video.original_filename

上传的文件名