我正在遇到有关Rails中的Authenticity Token的一些问题,因为我现在已经多次了.
但我真的不想只是解决这个问题而继续下去.我真的很想了解真实性令牌.那么,我的问题是,您是否有关于此主题的完整信息来源,或者您是否会花时间在此详细解释?
当我遇到一些真实性令牌问题时,我正在研究一个新的Rails 4应用程序(在Ruby 2.0.0-p0上).
在编写响应json的控制器时(使用respond_to类方法),当我尝试使用创建记录时,我create开始ActionController::InvalidAuthenticityToken尝试异常curl.
我确定我设置-H "Content-Type: application/json"并设置数据,-d "<my data here>"但仍然没有运气.
我尝试使用Rails 3.2(在Ruby 1.9.3上)编写相同的控制器,并且我没有任何真实性令牌问题.我四处搜索,看到Rails 4中的真实性令牌发生了一些变化.据我所知,它们不再自动插入表格了?我想这会以某种方式影响非HTML内容类型.
有没有办法解决这个问题,而无需请求HTML表单,抢夺真实性令牌,然后使用该令牌发出另一个请求?还是我完全错过了一些非常明显的东西?
编辑:我刚尝试使用脚手架在新的Rails 4应用程序中创建一个新记录而不改变任何东西,我遇到了同样的问题所以我想这不是我做的事情.
我正在使用devise注册/进入.
路线
get 'profile' => 'profile#get_profile'
post 'profile' => 'profile#create_profile'
Run Code Online (Sandbox Code Playgroud)
和profile_controller
def get_profile
render json: {user: current_user}, status: :ok
end
def create_profile
render json: {user: current_user}, status: :ok
end
Run Code Online (Sandbox Code Playgroud)
GET: http:// localhost:3000/user/profile返回预期的输出.然而,
POST请求引发错误说:
ActionController::InvalidAuthenticityToken in User::ProfileController#create_profile.
请揭开这种行为的神秘面纱.
我正在尝试使用react_on_railsreact和rails构建我的第一个示例.我正在尝试将一些数据保存到rails后端,使用axios for ajax.
这是我的代码:
import store from "../store/helloWorld";
import axios from "axios";
export const SAVE_NAME = "SAVE_NAME";
export function saveNameAction(name) {
return {
type: SAVE_NAME,
name
};
}
export function saveName(name) {
axios
.post("/hello_world", saveNameAction(name))
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
}
Run Code Online (Sandbox Code Playgroud)
和组件:
import PropTypes from "prop-types";
import React from "react";
import * as actions from "../actions/helloWorld";
export default class HelloWorld extends React.Component {
static propTypes = {
name: PropTypes.string.isRequired // this is passed from the …Run Code Online (Sandbox Code Playgroud)