Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a submit button image to a Rails form

I'm creating a form in Rails for submitting comments, and I want the submit button to be an image, rather than an HTML button. In this bit of documentation, it says the code is image_submit_tag, but I can't get it to work in my code.

Here's what I'm working with:

<% form_for [@post, Comment.new], :remote => true do |f| %>
<p>
    <%= f.label :body, "Add a comment" %><br />
    Name <%= f.text_field :name %><br />
    Website<%= f.text_field :website %><br />
    Twitter<%= f.text_field :twitter %><br />
    <%= f.text_area :body %>
</p>
<div id="comment-form">

    <div id="comment-button"><p>
        <%= f.image_submit_tag("comment-button.png") %></p>
        </div>
</div>
<% end %>

Thanks for the help.

like image 737
rottendevice Avatar asked Mar 05 '11 03:03

rottendevice


2 Answers

I just fell over this one, trying to solve the same problem. A sudden thought made me just try something like this:

<%= f.submit "Comment", :type => :image, :src => "/images/comment-button.png" %>

Will create something like this:

<input id="comment_submit" name="commit" src="/images/comment-button.png" type="image" value="Comment" />

Try it out :-)

like image 195
Tornskaden Avatar answered Sep 17 '22 17:09

Tornskaden


I believe the 'tag' methods cannot be called on the form builder object.

By 'tag' methods I mean things from the ActionView::Helpers::FormTagHelper module.

It should work if you do:

<div id="comment-button"><p>
  <%= image_submit_tag("comment-button.png") %></p>
</div>
like image 32
Kyle d'Oliveira Avatar answered Sep 18 '22 17:09

Kyle d'Oliveira