`

rails3.1 的link_to中delete无效的问题解决

阅读更多
现在才开始rails之旅有点晚,最近在捣鼓,发现变化确实很大,下面遇到的众多问题中的一个:
link_to 删除的操作无法正常进行,就是下面这行代码

<%= link_to "删除", article_path(article.id), :method => :delete, :confirm=>"are you sure?" %>


在rails2上没有任何问题,在用默认的prototype那一系列的js也可能没问题,关键是我卸载来prototype,安装的jquery。

首先是 点击 删除 链接之后,跳到了show页面,完全没有弹出框提示,造成这个问题的原因是一个低级错误,jquery的js文件没有引入

<%= javascript_include_tag "jquery.min" %>
<%= javascript_include_tag "jquery_ujs" %>(这个文件是关键)


引入之后弹出框生效来(说明生成的html代码中的自定义属性被解析来),但是删除仍然是无法进行,日志中显示了下面这一句提示:

WARNING: Can't verify CSRF token authenticity


先前,我曾经把link_to改成button_to,这个是完全可以操作的,button_to的原理是生成一个form表单,这个表单是自带token authenticity,这里 token authenticity 的警告正好说明link_to所缺少的东西,我的destroy action要经过一个登录验证,每次点击删除都是进入登录页面,误导我因为哪里的代码写错了删除来session,检查代码无误后,才明白是token authenticity的问题。

添加这一行代码到layout中
<%= csrf_meta_tag %>

解析生成的html代码如下:
<meta content="authenticity_token" name="csrf-param">
<meta content="wItbwqqVKfksJM4XuF8JteFBpSMhgr3IRYWLCmyXi2c=" name="csrf-token">

这个就完全解决了,删除操作提交的token问题

关于token的问题,在stackflow有一个很好的解释
http://stackoverflow.com/questions/941594/understand-rails-authenticity-token
原文贴出来:
引用
What happens: When the user views a form to create, update, or destroy a resource, the rails app would create a random authenticity_token, store this token in the session, and place it in a hidden field in the form. When the user submits the form, rails would look for the authenticity_token, compare it to the one stored in the session, and if they match the request is allowed to continue.

Why this happens: Since the authenticity token is stored in the session, the client can not know its value. This prevents people from submitting forms to a rails app without viewing the form within that app itself. Imagine that you are using service A, you logged into the service and everything is ok. Now imagine that you went to use service B, and you saw a picture you like, and pressed on the picture to view a larger size of it. Now, if some evil code was there at service B, it might send a request to service A (which you are logged into), and ask to delete your account, by sending a request to http://serviceA.com/close_account. This is what is known as CSRF (Cross Site Request Forgery).

If service A is using authenticity tokens, this attack vector is no longer applicable, since the request from service B would not contain the correct authenticity token, and will not be allowed to continue.

Notes: Keep in mind, rails only checks POST, PUT, and DELETE requests. GET request are not checked for authenticity token. Why? because the HTTP specification states that GET requests should NOT create, alter, or destroy resources at the server, and the request should be idempotent (if you run the same command multiple times, you should get the same result every time).

Lessons: Use authenticity_token to protect your POST, PUT, and DELETE requests. Also make sure not to make any GET requests that could potentially modify resources on the server.

========================================================================================
旧文分割线
=========================================================================================
如今已经升级到3.2,情况发生了变化,只需要安装jquery-rails即可(另外layout中csrf_meta_tag 也是需要的)
引用

Installation
Apps generated with Rails 3.1 or later include jquery-rails in the Gemfile by default. So just make a new app:

rails new myapp

If upgrading from an older version of rails, or for rails 3.0 apps, add the jquery-rails gem to your Gemfile.

gem "jquery-rails"

And run bundle install. The rest of the installation depends on whether the asset pipeline is being used.

Rails 3.1 or greater (with asset pipeline enabled)

The jquery and jquery-ujs files will be added to the asset pipeline and available for you to use. If they're not already in app/assets/javascripts/application.js by default, add these lines:

//= require jquery
//= require jquery_ujs

For jQuery UI, we recommend the jquery-ui-rails gem, as it includes the jquery-ui css and allows easier customization. This gem still packages the jQuery UI javascript for compatibility. To use it, add the following line to your application.js:
//= require jquery-ui

In order to use the themed parts of jQuery UI, you will also need to supply your own theme CSS (or use the jquery-ui-rails gem mentioned above).

Rails 3.0 (or greater with asset pipeline disabled)

This gem adds a single generator: jquery:install. Running the generator will remove any Prototype JS files you may happen to have, and copy jQuery and the jQuery-ujs driver for Rails (and optionally, jQuery UI) to the public/javascripts directory.

This gem will also hook into the Rails configuration process, removing Prototype and adding jQuery to the javascript files included by the javascript_include_tag(:defaults) call. While this gem contains the minified and un-minified versions of jQuery and jQuery UI, only the minified versions are included in :defaults.

To invoke the generator, run:

rails generate jquery:install #--ui to enable jQuery UI

You're done!
分享到:
评论

相关推荐

    Rails 3.1 实战

    这是最新的Rails 3.1 实战,包括TDD、BDD等实用技术。

    Rails link_to 详解

    = link_to “Issues”, {:controller =&gt; “issue”, :action =&gt; “index”}, {:onclick =&gt; “alert&#40;1&#41;”} %&gt; 但是值得注意的就是,link_to 的第二个参数,必须是以 map 的形式给出,才能达到这样的效果。...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    rails3.1安装与mysql配置 windows

    由于在Windows上直接安装可能会遇到问题,可以尝试使用预编译的二进制版本,或者在命令行中运行`gem install mysql2 --platform=ruby -- --with-mysql-dir="C:\path\to\mysql\installation"`,其中"C:\path\to\mysql...

    link_to_active_state:一个简单的gem,使用标准的Rails link_to helper在链接上实现活动状态

    一个简单的gem,可以使用标准的Rails link_to helper在链接上实现活动状态。 这对导航列表或按钮有帮助,当当前URL匹配链接帮助器上的条件时,为他们提供一个类。 安装 将此行添加到您的应用程序的Gemfile中: gem...

    rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器.zip

    rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...

    rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板.zip

    rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...

    inspinia admin - v2.5 Rails_Full_Version

    "inspinia admin - v2.5 Rails_Full_Version" 是一个基于Rails框架构建的后台管理系统的完整版本。这个系统采用流行的Inspinia Admin模板,提供了丰富的功能和自定义选项,旨在帮助开发者快速构建高效、现代且用户...

    Agile Web Development with Rails, 4th Edition, Rails 3.1

    ### 敏捷Web开发与Rails 4th Edition (Rails 3.1)——全面解析 #### 核心知识点概述 《敏捷Web开发与Rails》第四版涵盖了最新的Rails 3.1版本,是一本旨在帮助开发者快速掌握Ruby on Rails框架的核心书籍。本书...

    InspiniaAdmin 2.6.1 Rails_Full_Version

    7. **文档齐全**:InspiniaAdmin提供了详细的文档和示例代码,有助于开发者快速上手和解决遇到的问题。 在Rails_Full_Version压缩包中,包含了完整的源代码和必要的资源文件,开发者可以通过解压并导入到Rails项目...

    backbone-rails, 在 Rails 3.1中轻松使用 backbone.js.zip

    backbone-rails, 在 Rails 3.1中轻松使用 backbone.js 主干 rails 轻松设置并使用 Rails 3.1和更高版本的backbone.js ( 1.2.2 )##Version#####Github 主 branch###gem 版本:1.2.2Backbone 版本:1.

    RestFul_Rails_Dev_pdf_v_0.1.zip

    本资料“RestFul_Rails_Dev_pdf_v_0.1.zip”包含了《RESTful Rails Development》的翻译版,将深入探讨如何在Rails中实现RESTful的设计模式。 首先,RESTful设计的核心概念是资源(Resources)。在Rails中,资源...

    agile_web_development_with_rails_3rd_edition.9994652073.pdf

    由于Rails不断更新,新功能的引入或API的修改都可能导致代码兼容性问题。为了确保代码的正确运行,作者建议在使用本书示例代码之前,先检查自己所用的Rails版本是否与书中的版本一致。如果使用的是后续版本,应当...

    tiny_mce, Ruby on Rails 插件( pre Rails 3.1 ),允许你轻松地将TinyMCE编辑器实现到你的应用程序中.zip

    tiny_mce, Ruby on Rails 插件( pre Rails 3.1 ),允许你轻松地将TinyMCE编辑器实现到你的应用程序中 用于 Rails的 TinyMCE因为我没有时间维护它,而且代码库处于不一致状态,所以这个 gem 被折旧 ! 似乎如下笔记。...

    InspiniaAdmin 2.5 Rails_Seed_Project

    在这个Rails_Seed_Project中,开发者可以找到一个典型的Rails应用结构,包括Gemfile、config.ru、database.yml等核心配置文件,以及models、controllers、views和assets等目录,它们分别对应着业务逻辑、数据操作、...

    rails_best_practices:Rails项目的代码度量工具

    rails_best_practices rails_best_practices是用于检查Rails代码质量的代码度量工具。 它支持以下ORM / ODM: 活动记录 蒙古型 mongomapper 以及以下模板引擎: erb 哈姆 减肥 拉布尔 rails_best_practices...

    inspinia admin - v2.5 Rails_Seed_Project

    《Inspinia Admin - v2.5 Rails Seed Project:打造高效后台管理的全面解析》 在Web开发领域,后台管理界面的设计与实现是一项至关重要的任务,它直接影响到开发者的工作效率和用户体验。Inspinia Admin是一款广受...

    link_to_action:link_to用于特定操作

    LinkToAction 助手集:link_to_new,link_to_index,link_to_show,link_to_edit,link_to_destroy 额外帮手:link_to_back 启用了Twitter Bootstrap的演示应用程序源: 想要与一起使用此宝石吗? 提出。为什么Rails...

    ruby on rails 敏捷开发,3.1 pdf and epub format

    在Rails 3.1版本中,引入了许多重要更新和改进,使得这个框架更加高效且功能丰富。 首先,让我们深入了解Rails 3.1在资产管道(Asset Pipeline)方面的重要变革。资产管道是一种整合和优化应用程序中CSS、...

Global site tag (gtag.js) - Google Analytics