我想使用Jasny's Twitter Bootstrap extension设置我的Rails simple_form图像上传字段的样式。我已经(成功地)使用了CarrierWave上传图像。

目前,我的表单有效,并且代码看起来像这样(为了清楚起见,我删除了一些html,一些表单字段并设计了错误消息代码):

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {class: "form-horizontal", :method => :put }) do |f| %>

  <%= f.input :username, :label => "username" %>

  <%= f.simple_fields_for :picture do |pic| %>
    <%= pic.input :image, :as => :file, :label => "upload a photo" %>
  <% end %>

  <%= f.input :current_password, :label => "enter password to confirm update(s)" %>
  <%= f.button :submit, "Update", class: "btn btn-success" %>

<% end %>

“simple_fields_for:picture”部分产生以下HTML:
<div class="control-group file optional">
  <label class="file optional control-label" for="user_picture_attributes_image">
    upload a photo
  </label>
  <div class="controls">
    <input class="file optional" id="user_picture_attributes_image" name="user[picture_attributes][image]" type="file">
  </div>
</div>

要使用Jasny代码,我想也许可以用他们文档中的代码替换“simple_fields_for:picture”部分,只是“以绝望的尝试”,我已将其手动添加到了输入标签中:id =“user_picture_attributes_image“name =” user [picture_attributes] [image]“
<div class="fileupload fileupload-new" data-provides="fileupload">
  <div class="input-append">
    <div class="uneditable-input span3">
      <i class="icon-file fileupload-exists"></i>
      <span class="fileupload-preview"></span>
    </div>
    <span class="btn btn-file">
      <span class="fileupload-new">Select file</span>
      <span class="fileupload-exists">Change</span>
      <input type="file" id="user_picture_attributes_image" name="user[picture_attributes][image]"/>
    </span>
    <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
  </div>
</div>

它不起作用(duh:D)。我没有足够的技巧来深入了解Jasny的bootstrap-fileupload.js中的javascript部分到底是怎么回事,也不是在simple_form的幕后,所以我不知道是否可以在其中进行更改以使其正常工作。一些谷歌搜索告诉我有人创建了扩展名rails-jasny-bootstrap-extension,但是可悲的是,除了原始的css和js之外,还没有任何代码。
在这里很难绘制空白。

对于上下文:这里的资源是User。我的user.rb看起来像这样(相关代码):
class User < ActiveRecord::Base
  has_one :picture, as: :attachable, :dependent => :destroy
  accepts_nested_attributes_for :picture
end

我的图片模型如下所示:
class Picture < ActiveRecord::Base
  attr_accessible :image, :remote_image_url, :remove_image, :thumb_width, :thumb_height
  attr_accessible :attachable_id, :attachable_type
  belongs_to :attachable, polymorphic: true
  mount_uploader :image, ImageUploader
end

屏幕上所需差异的屏幕截图(忽略样式):

Link to Jasny's bootstrap-fileupload.zip
在此先感谢您的浏览,并在此致歉。让我知道是否需要添加其他信息。

(附言:如果有人知道一个简单的选择,那也将不胜感激。)

最佳答案

您可以尝试使用file.field代替输入。

从:

<%= f.simple_fields_for :picture do |pic| %>
  <%= pic.input :image, :as => :file, :label => "upload a photo" %>
<% end %>

至:
<%= f.simple_fields_for :picture do |pic| %>
  <%= pic.file_field :image %>
<% end %>

这不会从simple_form添加精美的表单助手。

关于javascript - 如何在Rails simple_form中实现Jasny的bootstrap文件上传样式扩展?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15604636/

10-11 09:08