开发Titanium Alloy JS安卓应用程序并拥有一个为验证API提供服务的rails 4.1应用程序。这是代码
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" = $1 LIMIT 1 [["email", "rt@c.com"]]
Unpermitted parameter: format
Completed 400 Bad Request in 5ms (Views: 0.2ms | ActiveRecord: 0.8ms)
当我点击过滤器时,我得到了这个
{"email"=>"rt@c.com", "password"=>"password"}
我尝试了多种方法,包括
在任何地方使用缩放器(即params.permit(:email))
使用符号或字符串访问has(这是由params.permit调用返回的无关访问的哈希
解
我正在使用Titanium Studio(钛合金)来开发这个,我正在渲染400的错误代码。我应该渲染一个401.我能够解析消息并在登录时向用户显示正确的错误。该代码在钛合金控制器中看起来像这样
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);
}
答案 0 :(得分:0)
你根本不需要在这里使用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])