如何在 rails 上生成 AuthenticityToken [英] how to generate AuthenticityToken on rails

查看:26
本文介绍了如何在 rails 上生成 AuthenticityToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己构建了表单标签,当我将表单发布到服务器时,它给了我一个 InvalidAuthenticityToken 错误,所以我想知道如何在当前情况下将它添加到我自己的:

I build the form tag by myself and when I post the form to server it give me a InvalidAuthenticityToken error, so I want to know how to add it in my own in current situation:

<form accept-charset="UTF-8" action="/crops/update" method="post">
  <input id="crop_x" name="crop_x" size="30" type="text" /><br />
    <input id="crop_y" name="crop_y" size="30" type="text" /><br />
  <input id="crop_w" name="crop_w" size="30" type="text" /><br />
   <input id="crop_h" name="crop_h" size="30" type="text" /><br />
  <input id="crop" name="crop" type="submit" value="Crop!" />
</form>

响应错误是:

ActionController::InvalidAuthenticityToken in CropsController#update 
ActionController::InvalidAuthenticityToken
Rails.root: /home/mlzboy/my/crop2
Application Trace | Framework Trace | Full Trace

推荐答案

有一个名为 form_authenticity_token 的视图助手,用于返回当前会话的真实性令牌.

There is a view helper called form_authenticity_token that returns the current session's authenticity token.

在您的 view.html.erb 中:

In your view.html.erb:

 <form action="/blah" method="POST">
   <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
   <input name="first_name" type="text">
 </form>

这篇关于如何在 rails 上生成 AuthenticityToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆