我在rails中的会话有问题,我也在使用backbone.js - 看起来rails正在寻找一个名为/sessions.js的路由,此外,当我查看控制台时我可以在网络响应中看到NoMethodError in SessionsController#create
。此时用户无法登录该站点,但我可以在控制台中看到会话ID。
class SessionsController < ApplicationController
respond_to :html #:json
def new
end
def create
user = User.find_by(email: params[:sessions][:email])
if user && user.authenticate(params[:sessions][:email])
session[:current_user_id] = user.id
log_in user
@notice = true
else
@notice = false
end
respond_to do |format|
format.html {render(:text => "not implemented")}
# format.js
end
end
# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
# Remembers a user in a persistent session.
# def remember(user)
# user.remember
# cookies.permanent.signed[:user_id] = user.id
# cookies.permanent[:remember_token] = user.remember_token
# end
#destroys user's session - logout
def destroy
log_out if logged_in?
redirect_to root_path
end
end
这是我的会话助手:
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# returns current user to be easily accessed later
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# is the user even logged in?
def logged_in?
!current_user.nil?
end
def log_out
session.delete(:user_id)
@current_user = nil
end
end
不确定这是否是问题的一部分,但这是我在Backbone中使用session的视图:
App.Views.LoginView = Backbone.View.extend({
el: '#login-view',
events: { 'submit form': 'authorize' },
authorize: function () {
// if (this.submitButton.hasClass('disabled') && !(this.form.data('user-authorized') === true)) {
// return false;
// } else {
// this.submitButton.addClass('disabled');
// }
var self = this,
attrs = {
email: this.emailField.val(),
password: this.passwordField.val()
};
User.authorize(attrs, function (err, user) {
if (err) { self.loginFailure(); }
else { self.loginSuccess(); }
});
return (this.form.data('user-authorized') === true);
},
loginSuccess: function () {
this.form.data('user-authorized', true);
this.form.submit();
},
loginFailure: function () {
this.$el.animate({left: '-=20'}, 100);
this.$el.animate({left: '+=40'}, 100);
this.$el.animate({left: '-=40'}, 100);
this.$el.animate({left: '+=40'}, 100);
this.$el.animate({left: '-=20'}, 100);
this.emailField.focus();
},
initialize: function () {
this.template = HandlebarsTemplates['login'];
this.render();
this.form = this.$el.find('form');
this.emailField = this.$el.find('#login-email');
this.passwordField = this.$el.find('#login-password');
this.submitButton = this.$el.find('#login-button');
},
render: function(){
this.$el.html(this.template);
$('#login-view').append(this.$el);
}
});
感谢任何帮助。
在NETWORK选项卡内,在控制台中:
NoMethodError in SessionsController#create
undefined method `[]' for nil:NilClass
<div class="source " id="frame-source-0">
<div class="info">
Extracted source (around line <strong>#9</strong>):
</div>
<div class="data">
<table cellpadding="0" cellspacing="0" class="lines">
<tr>
<td>
<pre class="line_numbers">
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
<span>11</span>
<span>12</span>
</pre>