Rails POST 到 API 不工作

Rails POST to API Not Working

正在开发一个 Titanium Alloy JS android 应用程序并有一个 rails 4.1 应用程序正在为 API 提供身份验证服务。这是代码

  def valid_login?
    ApiAuthenticator.new(user,filtered_params[:password]).valid?
  end

  def user
    @user ||= User.find_by(email: filtered_params[:email])
  end

  def filtered_params
    params.permit(:email, :password)
  end

我已经尝试了多种方法,但仍然出现此错误。

Started POST "/api/login" for 192.168.1.64 at 2015-02-27 16:52:52 -0600
Processing by Api::V1::LoginController#create as JSON
  Parameters: {"password"=>"[FILTERED]", "email"=>"rt@c.com"}
Unpermitted parameter: format
  [1m[35mUser Load (0.1ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."email" =  LIMIT 1  [["email", "rt@c.com"]]
Unpermitted parameter: format
Completed 400 Bad Request in 5ms (Views: 0.2ms | ActiveRecord: 0.8ms)

当我在 filtered_params 上做一个看跌期权时,我得到了这个

{"email"=>"rt@c.com", "password"=>"password"}

我已经尝试了多种方法,包括

  1. 到处使用缩放器(即 params.permit(:email))

  2. 使用符号或字符串访问 has(这是由 params.permit call

  3. 返回的具有无关访问的散列

解决方案

我正在使用 Titanium Studio (titanium alloy) 来开发它,我正在渲染不正确的错误代码 400。我应该渲染 401。我能够解析消息并显示正确的用户登录时出错。该代码在钛 alloy 控制器

中看起来像这样
function login() {
    var xhr=Titanium.Network.createHTTPClient();    
    xhr.open('POST', url_login);

    xhr.onload = function(){
     if(this.status == '200'){
            tokenResponse = JSON.parse(this.responseText);
            setToken(tokenResponse.token);          
            Alloy.Globals.logged_in = true;
            redirectToIssues();
     }        
    };

    xhr.onerror = function(e){
        tokenResponse = JSON.parse(this.responseText);
        alert(tokenResponse.message);
        };

    var params = {
        'email' : $.email.value,
        'password' : $.password.value
    };
     xhr.send(params);
}

实际上您根本不需要在这里使用 permit

  def valid_login?
    ApiAuthenticator.new(user,params[:password]).valid?
  end

  def user
    @user ||= User.find_by(email: params[:email])
  end

permit 仅在进行批量分配时有用,即 User.create(params[:user])