将按钮做为简单链接的方法是什么? [英] What's the rails way to do buttons as simple links?

查看:82
本文介绍了将按钮做为简单链接的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮(在haml视图中),我想使用该按钮转到另一个控制器/动作.我比Rails更习惯于PHP.我尝试过的方法奏效,但感觉不像铁轨.这是我的三种方式.谁能提供建议?我特别想看看带有javascript onclick处理程序的按钮的完成方式.

I have a button (in a haml view) that I want to use to go to another controller/action. I am more accustomed to PHP than rails. What I have tried works but does not feel like the rails way. Here are my 3 ways. Can anyone offer suggestions? I'd particularly like to see the way the button with a javascript onclick handler would be done.

%p
=link_to "Edit", edit_user_registration_path(@user)
%button.btn.btn-success.doEdit{:onclick => "window.location='#{edit_user_registration_path(@user)}'"} Edit
%button.btn.btn-danger.doEdit2 Edit
:javascript
  $(document).ready(function(){
  $('.doEdit2').click(function(event) {
      window.location="#{edit_user_registration_path(@user)}";
  });
});

推荐答案

我通常不会说一个按钮".我会这样做:

I would normally not use a "button" per say. I would do this:

=link_to "Edit", edit_user_registration_path(@user), class: "btn btn-danger edituser"

btn类将添加CSS样式以使其看起来像一个按钮,但它只是一个链接.要对此的javascript处理程序:

The btn class would add the CSS styling to make it look like a button, but it would just be a link. To javascript handler to this:

$("a.edituser").on("click", function(event) {
  event.stopPropagation()
  doWhateverYouNeedHere();
});

这篇关于将按钮做为简单链接的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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