Ruby on Rails - link_to button / css
link_to
generates a <a>
tag, which is not input type="submit"
. What you should use should be a button_to
, which generates a form with a input type="submit"
button to the link:
<%= button_to "Add", new_admin_course_path, :id => "open-contacts-dialog-btn",
:class => "inbox-sf-add-btn tip", :method => :get %>
Note the :method => :get
. Without it, the button_to
will generate a form with method set to post.
Rails link_to with default HTML button style
EDIT: I want to add that it is possible to use the button_to
helper, and it works like link_to
, but only when you aren't working with a form, because then the button_to
form will raise conflicts. This was an issue for me because the cancel button was triggering form validation on my form.
AFAIK this isn't possible to do with any built-in Rails components, so instead I opted to use HTML and Javascript to get what I wanted.
Old code:
<%= link_to 'Cancel', base_files_path() %>
New code:
<button type='button' onclick="location.href='<%= base_files_path() %>';">Cancel</button>
The code isn't pretty, but it's the quickest way for me to get the look I was going for.
rails button link action controller urlgeneration error
You're misunderstanding the URI you specify in the route with the controller's action.
If you write get '/join'
, this join
is the URI to be used to access your route, and if they point to home#clientsignup
, then you have a method called clientsignup
in your home
controller, but as I see, your method is join
, so you could try replacing this part, like:
get '/join', to: 'home#join', as: 'join'
And your link_to
should work. Although I think you could just use the path, to access your route, if you want to use less code, like:
<%= link_to 'List Space', join_path, class: 'button' %>
As join_path
is the alias your route takes, then Rails knows what controller and action it points to, no need to use controller
and action
as the link_to's options.
Most probably the other link_to helpers work because they match in the action being specified - I can't say exactly because they're not in the question.
use rails button in form as link
Why don't just use anchor tag
<a href='/main'>
<i class="icon icon-078-settings icon--s2" aria-hidden="true"></i>
</a>
or
# If you are using erb
<%= link_to '/main' do %>
<i class="icon icon-078-settings icon--s2" aria-hidden="true"></i>
<% end %>
rails 3: display link as button?
All you have to do is add a :method => "get"
to the end of your button_to to get it to be treated like a link
The button_to Way
Using a users_path
<%= button_to "BUTTON: link version", users_path, :method => "get" %>
The CSS Way
Or instead of actually inserting a form into your html (which is what button_to actually does) you could go with the cleaner (from a web design perspective) method and actually just style the link in a way to make it look like a button
This has several benefits
Here is a great article on how to do it and here is a little snippet of that code, really just depends on playing around with the border, padding and background image
a.button {
background: transparent url('bg_button_a.gif') no-repeat scroll top right;
color: #444;
display: block;
float: left;
font: normal 12px arial, sans-serif;
height: 24px;
margin-right: 6px;
padding-right: 18px; /* sliding doors padding */
text-decoration: none;
}
The code comes from the link above. Whichever method you choose should work fine, enjoy!
How to make a button work as a link in erb?
Just to throw another option out there since I had a scenario where the button_to option didn't work. This looks kind of similar to that.
<%= link_to '<button type="button">New Post</button>'.html_safe, new_post_path %>
What I basically wanted is a button that doesn't turn into a submit, since I have multiple buttons on the page that aren't related to a form, and I really just want it to just go to another page.
Make a link as a button in rails spree
If you want to generate an HTML button, instead of a hyperlink then change your link_to
to button_to
:
<%= button_to Spree.t(:forgot_password), spree.recover_password_path %>
This will generate a form with a single button in it that would take user to a given URL.
However, if you want to simply style your link as a button, then use CSS. The CSS rules will depend on your style. So, just Google and pick a style that fits your needs.
If your template is using Twitter Bootstrap, you can simply add btn
and btn-*
classes to style it as a button.
<%= link_to Spree.t(:forgot_password), spree.recover_password_path, class: 'btn btn-default' %>
Related Topics
How to Convert Ppt to Images in Ruby
How to Memoize a Method That May Return True, False, or Nil in Ruby
Ruby on Rails 4 Select Multiple
Stop Loading Page Watir-Webdriver
Rails Server Does Not Start by Command "Rails S"
Rails 3.1 Load CSS in Particular Order
Consequences of Implementing To_Int and To_Str in Ruby
Bundler - Load Multiple Gemfiles
Uploading Files in Ruby on Rails
What Does "Temps.Each(&:Valid)" Mean in Ruby
Rails Server Cannot Start; Getaddrinfo: Nodename Nor Servname Provided, or Not Known (Socketerror)
Problem Using Openstruct with Erb
Load Two Ruby Modules/Gems with the Same Name
No Such File to Load -- SQLite3/Sqlite3_Native
How to Render File in Rails 5 API
Rails 3.1 - Changing Default Scaffold Views and Template