我在Ruby中有一些身份验证(来自这个教程 --使用gem "bcrypt-ruby", :require => "bcrypt",我需要将authenticity_token写到json文件中。我怎样才能得到它并创建这个文件?
更新:我写了:
json.array!(@session) do |session|
json.extract! session, :csrf-token
json.url tag_url(session, format: :json)
end但不起作用。我需要从html写
<meta content="authenticity_token" name="csrf-param" /> <meta content="74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=" name="csrf-token" />此值: 74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=
这是我的session_controller:
class SessionsController < ApplicationController
def new
end
def index
@session
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end发布于 2014-04-02 20:25:30
有一个名为form_authenticity_token的视图助手,它访问或分配令牌,如下所示
def form_authenticity_token
session[:_csrf_token] ||= SecureRandom.base64(32)
end若要在控制器中获取它,请使用session[:_csrf_token]
https://stackoverflow.com/questions/22821368
复制相似问题