Cyz*_*far 5 ruby-on-rails twitter-bootstrap
我想更改 Rails 应用程序中默认的“选择文件”按钮。我正在使用Bootstrap,并且想用Bootstrap Glyphicons 之一替换该按钮。

div这
是class form-group控制“选择文件”按钮的
<div class="form-group">
<%= f.label :image %><br>
<%= f.file_field :image, class: "form-control" %>
</div>
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
您需要知道的是,这不是Rails问题 - 这是一个HTML问题。虽然这是可能的,但它更像是一种黑客行为,而不是真正的定制
一个很好的例子是我们在这里制作的演示 (您必须注册并单击右上角的“个人资料”按钮):

自定义file input按钮实际上并不像您想要的那样可行 - 您基本上需要隐藏按钮并将其替换为您自己的元素,然后可以将其绑定到 JS 事件。
--
方法
Pavan在评论中发布了一个史诗般的JSFiddle - 支持他!
我们用于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');
});
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7900 次 |
| 最近记录: |