首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RoR :按钮而不是link_to不能转到同一页

RoR :按钮而不是link_to不能转到同一页
EN

Stack Overflow用户
提问于 2012-03-22 01:54:45
回答 2查看 891关注 0票数 0

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

代码语言:javascript
复制
<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服务器上看到以下日志:

代码语言:javascript
复制
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)

我的路由如下:

代码语言:javascript
复制
[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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-22 02:05:52

尝试将您的按钮重写为

代码语言:javascript
复制
<form><a href=/home/index><button type="button">Main Menu</button></a></form>
票数 0
EN

Stack Overflow用户

发布于 2012-03-22 02:08:21

你的“主菜单”按钮提交了另一个定义好的表单。

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

代码语言:javascript
复制
a.button {
  border: 4px outset;
  padding: 2px;
  text-decoration: none;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9810153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档