ruby-on-rails - 更改默认 "choose file"按钮 rails

标签 ruby-on-rails twitter-bootstrap

我想更改我的 Rails 应用程序中的默认“选择文件”按钮。我正在使用 Bootstrap 并希望将按钮替换为 之一 Bootstrap 字形。

enter image description here

这里是 divclass form-group控制“选择文件”按钮

<div class="form-group">
    <%= f.label :image %><br>
    <%= f.file_field :image, class: "form-control" %>
</div>

最佳答案

HTML

你需要知道的是这不是 Rails问题 - 这是一个 HTML 一。虽然这是可能的,但它更像是一种 hack 而不是真正的定制

一个很好的例子是a demo we made here
(您必须注册并点击右上角的“个人资料”按钮):

enter image description here

自定义file input按钮实际上并不像您想要的那样可行 - 您基本上需要隐藏按钮并用您自己的元素替换它,然后可以将其绑定(bind)到 JS 事件。

--

方法

帕万 发表史诗JSFiddle在评论中 - 给他的 Prop !

我们使用了 jquery-file-upload 对于我们的按钮 - 基本上允许我们同时选择文件和上传。真的很棒:

#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');
            });
        });
    }

});

关于ruby-on-rails - 更改默认 "choose file"按钮 rails ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24716812/

相关文章:

html - 将最小高度应用于列表项

html - 使用 Bootstrap 在模态内的表格中保持适当的列宽

ruby-on-rails - Ruby/Rails 中的类方法与常量

ruby-on-rails - 如何在不转义反斜杠的情况下在 Rails 中输出 JSON

ruby-on-rails - Facebook 应用程序 - 通过 omniauth 登录 - OAuthException 191

ruby-on-rails - 如何在生产模式下部署Rails?

drop-down-menu - Bootstrap 下拉菜单不起作用

ruby-on-rails - 带关联的 Ruby on Rails Activemodel

html - Bootstrap 部分不会出现在移动浏览器中,但会出现在桌面版本中

html - 如何使切换按钮透明