4

I would like to change the default "choose file" button in my Rails app. I am using Bootstrap and would like to replace the button by one of the Bootstrap Glyphicons.

enter image description here


Here is the div of class form-group that controls the "choose file" button

<div class="form-group">
    <%= f.label :image %><br>
    <%= f.file_field :image, class: "form-control" %>
</div>
Zanon
  • 29,231
  • 20
  • 113
  • 126
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81

2 Answers2

3

HTML

Something you need to know is this is not a Rails issue - it's an HTML one. Although it is possible, it's more of a hack than a real customization

A good example is a demo we made here (you'll have to register & click on the "profile" button at top right):

enter image description here

Customizing the file input button isn't actually viable as you'd want - you basically need to hide the button & replace it with your own element, which can be then bound to a JS event.

--

Methods

Pavan posted an epic JSFiddle in the comments - props to him!!

We used jquery-file-upload for our button - basically allows us to select the file & upload at the same time. Truly amazing:

#app/views/your_controller/view.html.erb
<div class="avatar">        
        <!-- New Avatar Upload Form -->
        <%= form_for :upload, :html => {:multipart => true, :id => "avatar"}, :method => :put, url: profile_path(current_user.id), "data_id" => current_user.id do |f| %>
            <div class="btn btn-success fileinput-button avatar" id="avatar_container">
                <%= f.file_field :avatar, :title => "Upload New" %>
                <%= image_tag(@user.profile.avatar.url, :width=> '100%', :id => "avatar_img", :alt => name?(@user)) %>
            </div>
        <% end %>
 </div>

#app/assets/javascripts/application.js -> allows to upload directly (no submit)
$('#avatar').fileupload({

    url: '/profile/' + $(this).attr('data_id'),
    dataType: 'json',
    type: 'post',

    add: function (e, data) {
        //$(".items .avatar .avatar").prepend('<div class="loading" id="avatar_loading"><img src="<%= asset_path("profile/avatar_loading.gif") %>"></div>');
        //$('#avatar_loading').fadeIn('100');
        $(this).avatar_loading('avatar_loading');
        data.submit();
    },
    success: function (data, status) {;
        $("#avatar_img").fadeOut('fast', function() {
            $(this).attr("src", data.avatar_url).fadeIn('fast', function(){
                $(this).avatar_loading('avatar_loading');
            });
        });
    }

});
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
-6

RoR file_field not very customizable. You have to use some "magic".

This article solve your problem. But you can also use simple html style.

<input type="file" class="btn btn-small btn-primary btn-inverse" />

or

<input type="file" class="input-file" />
Chrissx
  • 357
  • 1
  • 7
  • Yes, beware that Chrome does not follow published web standards, so like the old IE days, one needs "special" code "just for Chrome". – JosephK Dec 30 '15 at 11:10