【发布时间】:2014-07-18 15:08:06
【问题描述】:
我正在尝试使用 Rails 3 + Angular.js,但是当我尝试使用 angular.js 向 Rails 发送发布请求时,控制器丢失了 current_session。
这是我的应用程序的工作流程。
1.使用 devise 登录(只能使用 rails 方式)
2。应用程序被重定向到 crear_perfil.html(这是一个 angular.js 视图)
这是我的 app.js
//= require angular
//= require angular-animate
//= require angular-resource
//= require angular-route
//= require angular-touch
//= require_tree ./angular/models
//= require_tree ./angular/controllers
//= require_tree ./angular/directives
//= require underscore-min
//= require angular-google-maps.min
app = angular.module('squapeApp', ['ngAnimate' , 'ngRoute', 'ngResource','PublicationService','PersonService','appCtrl', 'google-maps', 'ngTouch']);
app.config(['$routeProvider','$locationProvider',
function($routeProvider,$locationProvider){
$routeProvider.
when('/publicaciones',{
templateUrl: '../assets/index.html',
controller: 'PublicationCtrl'
}).
when('/informacion/perfil',{
templateUrl: '../assets/crear_perfil.html',
controller: 'ProfileCtrl'
}).
otherwise({
redirectTo: '/publicaciones'
});
$locationProvider.html5Mode(true);
}]);
这是我这一步的角度视图:
<form name="profileForm" ng-submit="profileForm.$valid && submitForm()" novalidate>
<input class="text input" type="text" placeholder="Nelson Patricio" id="nombre" ng-model="person.name" required/>
<input type="submit" name="" value="Guardar" ng-disabled="profileForm.$invalid">
</form>
3.填写该字段并将表单提交给 angular.js 控制器( ProfileCtrl )
这是我的 appCtrl.js(这个文件内容是我的 ProfileCtrl)
var app = angular.module("appCtrl", []);
app.controller('ProfileCtrl', ['Person','$scope','$http', function(Person, $scope, $http){
$scope.angularClass = "e-profile";
$scope.person = new Person();
$scope.persona = Person.query();
$scope.submitForm = function(){
if ($scope.profileForm.$valid) {
console.log("waiting for save");
$scope.person.$save();
console.log("save it");
}
};
}]);
4. ProfileCtrl 调用 Person 工厂
var person = angular.module('PersonService', []);
person.factory('Person', ['$resource', function($resource){
var Person = $resource(
'http://squape.dev/perfil/:id.json',
{id: '@id'}, {
update: {
method: 'PUT'
},
query:{
isArray: false
}
});
return Person;
}]);
5. Person Factory 调用 rails 控制器(ProfileController)
class ProfileController < ApplicationController
respond_to :json
def index
@person = current_user.person
respond_with @person
end
def create
@person = current_user.build_person(params[:person])
if @person.save
respond_with @person, location: root_path
end
end
def update
redirect_to root_path
end
def informacion
@user = current_user
@user.build_person if @user.person.nil?
end
end
当我发送 angularjs FORM(angularjs 发送 POST 请求,因此在 rails 控制器中调用 create 方法)时,我的 curren_session 为 nil,但如果我调用 angular 查询方法(angularjs 发送 GET 请求,因此 index 方法在 rails 控制器中调用)current_user 是一个很好的对象。
为什么我发送 POST 请求时,rails 控制器中的 create 方法返回 current_user = nil ?
非常感谢您的建议。
问候,
**更新 *********
问题解决了,current_user 为 nil,因为我没有发送 X-CSRF-Token,为了解决这个问题,在我的 app.js 中添加这个。
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
app = angular.module('squapeApp', ['ngAnimate' , 'ngRoute', 'ngResource','PublicationService','PersonService','appCtrl', 'google-maps', 'ngTouch']);
app.config(['$routeProvider','$locationProvider', '$httpProvider',
function($routeProvider,$locationProvider, $httpProvider){
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
$routeProvider.
when('/publicaciones',{
templateUrl: '../assets/index.html',
controller: 'PublicationCtrl'
}).
when('/informacion/perfil',{
templateUrl: '../assets/crear_perfil.html',
controller: 'ProfileCtrl'
}).
otherwise({
redirectTo: '/publicaciones'
});
$locationProvider.html5Mode(true);
}]);
但现在控制器创建了一个 void 对象。出于任何原因没有收到姓名和姓氏
【问题讨论】:
-
我猜是因为你没有发送真实性令牌
-
非常感谢,我添加了 X-CSRF-Token 并接受了 POST 请求,但现在控制器中的参数有问题。控制器正在创建 nil 对象
标签: ruby-on-rails ruby-on-rails-3 angularjs