在带有超链接的rails中发出POST请求 [英] making POST request in rails with hyperlinks

查看:135
本文介绍了在带有超链接的rails中发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在页面上有一堆链接,每个链接都会向不同的控制器发出POST。但是当我使用普通链接时,我得到一个ActionController :: InvalidAuthenticityToken错误。我理解这是因为缺少authenticity_token值。但我不想使用表单来进行POST,因为我希望它们是链接而不是按钮。事实是,我想完全控制链接和按钮的样式,只是不要为我做。做这些事情的标准方法是什么?

I need a bunch of links on a page each of which makes a POST to a different controller. But when I use normal links, I get a ActionController::InvalidAuthenticityToken error. I understand this is because of the missing authenticity_token value. But I don't want to use forms to do the POST because I want them to be links and not buttons. Fact is, I want complete control over the styling of the links and buttons just don't do it for me. What's the standard way of doing such things?

推荐答案

你有很多选择。


  1. 禁用AT检查: protect_from_forgery:only => []

  2. 为链接指定onclick处理程序并使用javascript提交隐藏表单。

  3. 在生成视图时抓取AT将其添加为请求参数。

BTW,您如何仅使用'href'属性来发布请求?我认为形式是必须的。

BTW, how exactly do you make 'post' requests using only 'href' attribute? I thought form is a must for that.

这篇关于在带有超链接的rails中发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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