What's the rails way to do buttons as simple links?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 05:26:52

问题


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)}";
  });
});

回答1:


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"

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();
});



回答2:


button_to is probably what you're after.



来源:https://stackoverflow.com/questions/11007831/whats-the-rails-way-to-do-buttons-as-simple-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!