【问题标题】:RoR : button instead of link_to does not go to the same pageRoR :按钮而不是 link_to 不会转到同一页面
【发布时间】:2012-03-21 17:54:45
【问题描述】:

在 Ruby on Rails 表单中,我尝试使用按钮而不是 link_to 返回主菜单。我使用了以下内容(我在浏览这个论坛时发现的):

<form><a href=/home/index><button>Main Menu</button></a></form>

如果我将鼠标悬停在按钮上,我会看到 http://172.19.95.56:3005/home/index,这是我可以在浏览器上看到的有效页面,这就是我希望按钮将我带到的地方,但如果我单击该按钮,它需要我到http://172.19.95.56:3005/l2vpns/1/edit,我在 Rails 服务器上看到以下日志:

Started GET "/l2vpns/1/edit" for 172.24.67.151 at 2012-03-21 10:44:26 -0700
Processing by L2vpnsController#edit as HTML
Parameters: {"id"=>"1"}
L2vpn Load (0.4ms)  SELECT "l2vpns".* FROM "l2vpns" WHERE "l2vpns"."id" = ? LIMIT 1  [["id", "1"]]
Rendered l2vpns/_form.html.erb (65.7ms)
Rendered l2vpns/edit.html.erb within layouts/application (66.5ms)
Completed 200 OK in 81ms (Views: 76.6ms | ActiveRecord: 1.2ms)

我的路线如下:

[root@localhost pocplus]# rake routes
apply_configs_l2vpns GET    /l2vpns/apply_configs(.:format)        {:action=>"apply_configs", :controller=>"l2vpns"}
          l2vpns GET    /l2vpns(.:format)                      {:action=>"index", :controller=>"l2vpns"}
                 POST   /l2vpns(.:format)                      {:action=>"create", :controller=>"l2vpns"}
       new_l2vpn GET    /l2vpns/new(.:format)                  {:action=>"new", :controller=>"l2vpns"}
      edit_l2vpn GET    /l2vpns/:id/edit(.:format)             {:action=>"edit", :controller=>"l2vpns"}
           l2vpn GET    /l2vpns/:id(.:format)                  {:action=>"show", :controller=>"l2vpns"}
                 PUT    /l2vpns/:id(.:format)                  {:action=>"update", :controller=>"l2vpns"}
                 DELETE /l2vpns/:id(.:format)                  {:action=>"destroy", :controller=>"l2vpns"}
      home_index GET    /home/index(.:format)                  {:controller=>"home", :action=>"index"}
                        /:controller(/:action(/:id(.:format))) 
            root        /                                      {:controller=>"home", :action=>"index"}
[root@localhost pocplus]# 

谁能告诉我我做错了什么?为什么按钮进入“编辑”页面而不是 /home/index 页面?

如果此信息很重要,我使用的是 Firefox 11.0

【问题讨论】:

    标签: ruby-on-rails button routes link-to


    【解决方案1】:

    尝试将您的按钮重写为

    <form><a href=/home/index><button type="button">Main Menu</button></a></form>
    

    大多数浏览器中按钮的默认类型是“提交”,因此它会尝试提交您的表单。

    【讨论】:

    • 谢谢niiru,这对我有用,但是按钮内的文本像超文本一样带有下划线,有没有办法不给文本加下划线?我知道这是装饰性的,但我希望我的所有按钮都一样。
    • 您可以将该链接的 CSS 文本装饰样式设置为无:
    • 我可以在 Firefox 中使用,但现在在 IE9 中无法使用;任何的想法 ?
    【解决方案2】:

    您的“主菜单”按钮在其中提交另一个表单。

    您根本不需要&lt;button/&gt;,只需使用简单的link_to "Main menu", home_index_path, :class =&gt; :button。然后在你的样式表中让你的链接看起来像按钮:

    a.button {
      border: 4px outset;
      padding: 2px;
      text-decoration: none;
    }
    

    【讨论】:

    • 感谢 Nash Bridges,我试过了,但它抱怨 home_path “未定义的局部变量或方法 `home_path' for #:0xb403804>”
    • @rh4games 当然,我应该更加专心。更正了 url 助手。
    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2012-09-22
    • 2013-07-06
    • 2011-11-07
    相关资源
    最近更新 更多