六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 143|回复: 0

link_to的用法

[复制链接]

升级  31.33%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
97
 楼主| 发表于 2013-2-7 19:48:06 | 显示全部楼层 |阅读模式
<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方法提交到后台,而是直接作为参数传递到后台。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表