Scott Eisenberg
Scott Eisenberg

Reputation: 429

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

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

Upvotes: 1

Views: 11946

Answers (2)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

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

Upvotes: 5

sczizzo
sczizzo

Reputation: 3196

button_to is probably what you're after.

Upvotes: 3

Related Questions