Rails 3 link_to generator for:post,:put,& :删除? [英] Rails 3 link_to generator for :post, :put, & :delete?

查看:124
本文介绍了Rails 3 link_to generator for:post,:put,& :删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这个问题: Rails 3 link_to(:method =>:删除)无法正常工作

I recently ran into this problem: Rails 3 link_to (:method => :delete) not working

我很困惑为什么我的 link_to'test',test_path(:test = > test),:method => :post 无效(为什么它是GETting而不是POSTing)。如上面的帖子所示,这是因为我没有Rails javascript文件,它可以动态地为动态制作表单:post :put & :delete

In which I was confused why my link_to 'test', test_path(:test => test), :method => :post wasn't working (why it was GETting rather than POSTing). As the post above suggests, it was because I didn't have the Rails javascript files, which handles dynamically making the form on-the-fly for :post, :put & :delete.

我不确定我是否特别喜欢依赖javascript为我这样做,因为我我希望我的应用程序能够在禁用javascript时完全正常运行;这就是我喜欢开发的方式(所以我知道我的所有Rails编程功能都能正常工作),当我用javascript连接我的应用程序时,我不必担心重复这个功能,因为我通常会擦除由Rails生成的股票javascripts文件。

I'm not sure I especially like relying on javascript to do this for me, as I'd like my app to be fully functional with javascript disabled; it's how I like to develop (so I know I've got all my Rails programming functionality works correctly), and when I do wire my app up with javascript, I don't have to worry about duplicating this functionality, since I usually wipe the stock javascripts file generated by Rails.

我从某处读到,从技术上讲,超链接(锚元素)应该只用于GET请求和那些按钮(作为一部分)形式元素)应该用于其他方法。

I read somewhere that, technically, hyperlinks (anchor elements) should only really be used for GET requests, and that buttons (as part of form elements) should be used for the other methods.

所以,我的两个问题:


  1. 那里有宝石吗?当我的视图呈现时,会在我的视图中将任何 link_to ...:method => 重写为表单元素吗?

  2. 如果没有,我将如何挂钩到视图解析器来编写自己的? (使用HAML,最好是)

  1. Is there a gem out there that will rewrite any link_to ... :method => in my view to a form element on-the-fly when my views are rendered?
  2. If not, how would I hook into the view parser to write my own? (using HAML, preferrably)

基本上,我想要的是在我的HAML文件中写这个:

Essentially, what I would like is to write this in my HAML file:

= link_to 'Test Link', some_path(:with => some_resource), :method => :post

...并且我的渲染HTML输出结果如下:

... and have my rendered HTML output come out with this:

<form action="some_path?with=some_resource" method="post">
  <!-- insert appropriate _method hidden field here -->
  <!-- add appropriate csrf token and whatnot -->
  <button type="submit">Test Link</button>
</form>

...以便所有 link_to 具有已定义方法的助手(因此,不会影响其动词为GET的普通链接)将在没有javascript 的情况下工作

... so that all the link_to helpers with defined methods (thus, will not affect plain links whose verb is GET) will work without javascript?

我也很感激有关为什么这个问题的一部分的想法,特别是为什么Rails团队决定将这个功能与javascript联系起来而不是通过纯HTML做这个?

I'd also appreciate any thoughts about the why part of this question, specifically why the Rails team decided to keep this functionality tied to javascript rather than doing this through pure HTML?

推荐答案

看起来你真的想要一个单一的元素形式(在这种情况下,一个按钮)。如果是这样,你想要 button_to 帮助器:

It seems that you really want a single element form (in this case, a button). If so, you want the button_to helper:

button_to'Test Link',some_path (:with => some_resource),:method => :post

这将生成一个带有输入的表单[type = submit] 它内部的元素。

This will generate a form with an input[type=submit] element inside of it.

如果没有Javascript,你就不能拥有使用GET以外的HTTP动词提交的超链接。

Without Javascript, you cannot have a hyperlink that will submit using a HTTP verb other than "GET."

如果您需要更多样式/控制由 button_to 呈现的表单,您可以执行以下操作:

If you need more style / control of the form being rendered by button_to, you can do something like this:

module LinksHelper
  def link_form(text, destination, options = {})
    form_tag(destination, :method => options.delete(:method)) do
      <<-HTML
        <!-- some HTML code here -->
        <button type="submit">#{text}</button>
        <!-- some more HTML code here -->
      HTML
    end
  end
end

这篇关于Rails 3 link_to generator for:post,:put,&amp; :删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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