相关疑难解决方法(0)

理解Rails真实性令牌

我正在遇到有关Rails中的Authenticity Token的一些问题,因为我现在已经多次了.

但我真的不想只是解决这个问题而继续下去.我真的很想了解真实性令牌.那么,我的问题是,您是否有关于此主题的完整信息来源,或者您是否会花时间在此详细解释?

ruby ruby-on-rails authenticity-token

959
推荐指数
10
解决办法
18万
查看次数

Rails 4真实性令牌

当我遇到一些真实性令牌问题时,我正在研究一个新的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应用程序中创建一个新记录而不改变任何东西,我遇到了同样的问题所以我想这不是我做的事情.

ruby ruby-on-rails authenticity-token ruby-on-rails-4

198
推荐指数
7
解决办法
13万
查看次数

如何忽略Rails中特定操作的真实性标记?

当我有一个特定的操作,我不想检查真实性标记时,如何告诉Rails跳过检查它?

ruby-on-rails

165
推荐指数
2
解决办法
7万
查看次数

帖子上的真实性令牌无效

我正在使用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.

请揭开这种行为的神秘面纱.

ruby ruby-on-rails devise ruby-on-rails-3

18
推荐指数
2
解决办法
2万
查看次数

与Rails 5做出反应,使用axios获得CSRF错误

我正在尝试使用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)

ajax ruby-on-rails csrf reactjs axios

6
推荐指数
1
解决办法
1919
查看次数