`
orcl_zhang
  • 浏览: 234328 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

link_to的用法

阅读更多
    <li>
      <%= link_to 'Logout',:controller => :account,:action=>:logout,:method=>:post %>
    </li>

提交logout的时候,总是卡着不动。
controller里去掉request.post之后,就可以。
但是request.post本身没有错。于是乎猜想是link_to的方法用错了,导致不是用post方法提交的。

进一步追查原因
状态栏显示:logout?method=post
html源码。
<a href="/account/logout?method=post">Logout</a>


看来应该是link_to用错了!

查了之前的用法,然后加了一个大括号!
    <li>
      <%= link_to 'Logout',{:controller => :account,:action=>:logout},:method=>:post %>
    </li>

状态栏显示:logout
html源码
f.method = 'POST';


ok通过了。

查看api
link_to(*args, &block)

Creates a link tag of the given name using a URL created by the set of options. See the valid options in the documentation for url_for. It‘s also possible to pass a string instead of an options hash to get a link tag that uses the value of the string as the href for the link, or use :back to link to the referrer - a JavaScript back link will be used in place of a referrer if none exists. If nil is passed as a name, the link itself will become the name.
Signatures

  link_to(name, options = {}, html_options = nil)
  link_to(options = {}, html_options = nil) do
    # name
  end

关键点在这里,
  link_to(name, options = {}, html_options = nil)
link_to的第只有三个参数,如果省略{}之后,最后的:method=>:post没有作为post方法提交到后台,而是直接作为参数传递到后台。
分享到:
评论
1 楼 orcl_zhang 2009-11-09  
一步留神上首页了....
我的神啊..我是刚学会rails....
隔了2个星期.我都觉得当时傻傻的..

相关推荐

Global site tag (gtag.js) - Google Analytics