5

I am trying to customize this part of code in my Rails3 application

<%= f.submit "Create my account", :class => "btn btn-small btn-primary btn-inverse" %>
<%= f.file_field :photo %>

into this:

<%= f.submit "Create my account", :class => "btn btn-small btn-primary btn-inverse" %>
<%= f.file_field :photo, :class => "btn btn-small btn-primary btn-inverse" %>

but the file_field doesnt not seem to respond to customization.

Can somebody help me in this one?

Nick
  • 9,493
  • 8
  • 43
  • 66
Test Test
  • 2,831
  • 8
  • 44
  • 64

3 Answers3

3

I used mindriot's suggestion, but needed to add one comma after the inline onchange js and couldn't edit his answer as one-character edits are not allowed:

<div class="input-group">
  <span class="input-group-btn">
    <span class="btn btn-small btn-primary btn-inverse" onclick="$(this).parent().find('input[type=file]').click();">Browse</span>
    <%= f.file_field :file_upload, onchange: "$(this).parent().parent().find('.form-control').html($(this).val().split(/[\\\\|/]/).pop());", style: "display: none;" %>
  </span>
  <span class="form-control"></span>
</div>
duhaime
  • 25,611
  • 17
  • 169
  • 224
0

the class are applied to the input and not the button

http://jsfiddle.net/baptme/k3KtH/

If you want to style the button you can use the method described here : http://www.quirksmode.org/dom/inputfile.html

baptme
  • 10,062
  • 3
  • 52
  • 57
0
<div class="input-group">
  <span class="input-group-btn">
    <span class="btn btn-small btn-primary btn-inverse" onclick="$(this).parent().find('input[type=file]').click();">Browse</span>
    <%= f.file_field :photo, onchange: "$(this).parent().parent().find('.form-control').html($(this).val().split(/[\\\\|/]/).pop());" style: "display: none;" %>
  </span>
  <span class="form-control"></span>
</div>

Based on this answer.

Community
  • 1
  • 1
mindriot
  • 14,149
  • 4
  • 29
  • 40
  • Missing comma on this response Mindriot. `" style: "display: none;" %>` should be `", style: "display: none;" %>` – Rene Chan Oct 07 '18 at 07:22