开发者

Ruby on Rails - link_to button / css

开发者 https://www.devze.com 2023-01-29 21:12 出处:网络
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

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 wou开发者_开发技巧ld 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


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.


I recently had to do this because my form was acting differently for link_to than it was with button_to and I needed the functionality the link provided. The best solution I found does not use CSS at all but instead calls .html_safe to escape the html in the ruby code and properly display the link as a button. What you want to do is this:

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


But it is not adding my CSS styling
What do you mean? The link (a element) with classes inbox-sf-add-btn and tip is generated, right? So, what's the problem?

If you want strictly button (input element instead of a), you can specify /book/new (or <%= new_admin_course_path %>) in the action attribute of your html form (form element).

0

精彩评论

暂无评论...
验证码 换一张
取 消