Ruby 2.6.0无效的真实性令牌

时间:2019-01-26 23:38:40

标签: ruby-on-rails ruby ruby-2.6

我的代码显示的是“无效的真实性”,而不是“缺少模板”。我需要在程序中进行哪些更改以获取“缺少模板错误”? img1 img2 img3 errorImg

下面是对整个程序的引用: link to github resp

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end

new.html.erb如下:     

新视图

<form action="/users" method="post" accept-charset="UTF-8">
  <label for="username">Username:</label><br>
  <input id="username" name="user[username]" type="text" /><br>
  <label for="email">Email:</label><br>
  <input id="email" name="user[email]" type="text" /><br>
  <label for="password">Password:</label><br>
  <input id="password" name="user[password]" type="text" /><br>
  <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>"> 
  <input type="submit" value="Submit">
</form>

route.rb

Rails.application.routes.draw do
  resources :users, only: [:new, :create]
end

2 个答案:

答案 0 :(得分:1)

好像您正在尝试使用config / application.rb来做protect_from_forgery

config.api_only = true

Here is your situation described

如果您将应用程序用作API,则应按以下方式重新生成它

$ rails new my_api --api

如果需要更高的安全性,则可以将令牌存储在其他位置(而不是cookie或会话)中-例如,可以使用JWT令牌。 为了提高安全性,您还可以使用rack-cors gem 而且,如果您不小心删除了资产并且不想使用API​​,则可以将此配置设置为false

答案 1 :(得分:0)

问题出在这一行:

db.query('my_index/by_name', {
  limit: 0 // don't return any results
}).then(function (res) {
  // index was built!
}).catch(function (err) {
  // some error
});

这实际上应该是:

 <input type="hidden" name="authenticity_token" value="form_authenticity_token %>">

否则,用于阻止chross站点请求伪造攻击的真实性令牌将只是“ form_authenticity_token%>”,而不是真实令牌。

相关问题