Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - link_to button / css

Ok so I am teaching myself RoR while developing a simple api & web interface. I have been following a number of guides and testing out different outcomes. I have mocked up some HTML templates and I am at the process where I am starting to strip them down into views.

In my HTML i have the following code, which is a Button with CSS styling:

 <input type="submit" name="" value="Add" id="open-contacts-dialog-btn" class="inbox-sf-add-btn tip" title="Open an dialog to add a new contact"/>   

This is my button in my panel that I would like to link to the /book/new/, I know i need a link_to in here but what would my final code be? The code I have tried is as follows

<%= link_to "Add", new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %> 

But it is not adding my CSS styling, it is just generating the text "Add". Any help would be useful. Thanks

like image 793
ChrisMJ Avatar asked Dec 11 '10 17:12

ChrisMJ


People also ask

How does Link_to work in Rails?

Rails is able to map any object that you pass into link_to by its model. It actually has nothing to do with the view that you use it in. It knows that an instance of the Profile class should map to the /profiles/:id path when generating a URL.


1 Answers

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.

like image 148
PeterWong Avatar answered Sep 23 '22 15:09

PeterWong