小编Flo*_*nce的帖子

使用Ajax将Ember Js的CSV文件发送到Rails

我正在尝试从Ember Js上传带有ajax的csv文件,并在我的Rails应用程序中读取它.我尝试了两种不同的方法.在第一个我尝试从Ember发送文件,如下所示:

submitImport() {
  var fd = new FormData();
  var file = this.get('files')[0];
  fd.append("csv_file", file);
  return this.get('authAjax')
    .request('/contacts/import/csv', {
      method: 'POST',
      processData: false,
      contentType: false,
      data: fd 
    });
}
Run Code Online (Sandbox Code Playgroud)

但问题是我没有在rails应用程序中获得csv_file参数.request.content_type是application/x-www-form-urlencoded,我需要多部分表单.我可以使用reques.raw_post然后我得到这样的东西------WebKitFormBoundarymgBynUffnPTUPW3l\r\nContent-Disposition: form-data; name=\"csv_file\"; filename=\"elevatr_import.csv\"\r\nContent-Type: text/csv\r\n\r\ngeorgica,gica@me.com\nleo, leonard@yahoo.com\ngigel, becali@oita.fcsb\n\r\n------WebKitFormBoundarymgBynUffnPTUPW3l--\r\n,我需要以某种方式解析这个,我真的不喜欢这个解决方案.

另一种方法是发送base64编码的文件,然后从Rails解码它.我试过这个:

`

submitImport() {
  var fd = new FormData();
  var file = this.get('files')[0];
  this.send('getBase64', file);
  var encoded_file = this.get('encoded_file');

  return this.get('authAjax')
    .request('/contacts/import/csv', {
      method: 'POST',
      data: { csv_file: encoded_file }
    });
},
getBase64(file) {
  var controller = this;
  var reader …
Run Code Online (Sandbox Code Playgroud)

ajax ruby-on-rails ember.js

6
推荐指数
1
解决办法
470
查看次数

Rails GrapeAPI + Authlogic未定义方法“会话”

我正在尝试使用GrapeAPI和构建用于登录的API Authlogic

我的代码如下所示:

class Api
  class V1
    class UserSessionsController < Grape::API


    post :login do
      @user_session = UserSession.new(params[:user_session])
      if @user_session.save
        fetch_api_key(@user_session.user)
      else
        error!('Unauthorized.', 401)
      end
    end

    helpers do
      def current_user_session
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = UserSession.find
      end

      def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session && current_user_session.user
       end

      end

    end
  end
end
Run Code Online (Sandbox Code Playgroud)

问题是当我跑步时@user_session = UserSession.new(params[:user_session])我会得到You must activate the Authlogic::Session::Base.controller with a controller object before creating objects

我尝试添加

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

在尝试创建之前UserSession …

ruby ruby-on-rails authlogic grape-api

5
推荐指数
0
解决办法
183
查看次数

标签 统计

ruby-on-rails ×2

ajax ×1

authlogic ×1

ember.js ×1

grape-api ×1

ruby ×1