载波问题-文件未显示? [英] Carrierwave Problem - Files not showing?

查看:78
本文介绍了载波问题-文件未显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <%@ company.comments.each做|评论| %> 
< tr>
< td><%= comment.commenter%>< / td>
< td><%= comment.body%>< / td>
< td><%= time_ago_in_words(comment.created_at, Comment)%> ago< / td>
< td><%= comment.commentfile%>< / td>
< / tr>
<%end%>

我正试图从下面的表单中显示上载的文件:

 < h2>添加评论:< / h2> 
<%= form_for([@ company,@ company.comments.build])做| f | %>
< div class = hidden>
名称:< br />
<%= f.text_field:commenter,:value => current_user.full_name,:readonly => 只读%>
< / div>
< div class = field>
评论:< br />
<%= f.text_area:body%>
< / div>
< div class = field>
<%= f.file_field:commentfile%>
< / div>
< div class = actions>
<%= f.submit%>
< / div>
<%end%>

但我不知道在检查我的public / uploads文件夹时是否正在保存文件没有文件出现。在<%= comment.commentfile%> 的视图中,我得到了我上传的文件的名称,但不知道文件在哪里或如何链接到还是文件甚至都没有上传?开始认为它只是插入了一个字符串。我的模型在下面。

  class Comment< ActiveRecord :: Base 
属性_to:联系人
属性_to:公司
mount_uploader:commentfile,CommentFileUploader
end

和comment_file_uploader.rb

  class CommentFileUploader< CarrierWave :: Uploader :: Base 

storage:file

def store_dir
uploads /#{model.class.to_s.underscore} /#{mount_as} /#{model.id}
结束
结束

请帮助!



还要注意我是否愿意

  u = Comment.new 
u.commentfile = params [:file]

在控制台中,我得到

  NameError:main:Object 


迁移添加了:commentfile

  class CreateUploader< ActiveRecord :: Migration 
def self.up
add_column:comments,:commentfile,:string
end

def self.down
end
end


解决方案

浏览器必须使用特殊格式将上传文件数据与表单数据一起过帐。您需要将表单分成多部分。

 <%= form_for([@company,@ company.comments.build], 
:html => {:multipart => true})做| f | %>

这会将属性enctype = multipart / form-data添加到生成的HTML和浏览器中然后应该可以在邮件的单独部分中发送上载的文件。



如果您使用Firebug或类似工具检查帖子数据,则会看到没有启用了多部分编码后,浏览器不会发送文件数据。


<% @company.comments.each do |comment| %>
  <tr>
    <td><%= comment.commenter %></td>
    <td><%= comment.body %></td>
    <td><%= time_ago_in_words(comment.created_at, "Comment") %> ago</td>
    <td><%= comment.commentfile %></td>
  </tr>
<% end %>

Is where i am trying to display the uploaded file from the form below:

<h2>Add a comment:</h2>
<%= form_for([@company, @company.comments.build]) do |f| %>
  <div class="hidden">
    Name:<br />
    <%= f.text_field :commenter, :value => current_user.full_name, :readonly => "readonly" %>
  </div>
  <div class="field">
    Comment:<br />
    <%= f.text_area :body %>
  </div>
  <div class="field">
    <%= f.file_field :commentfile %>  
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

but I don't know whether the file is being saved beacause when i check my public/uploads folder no files appear. And in the view at <%= comment.commentfile %> I get the name of the file i uploaded but no idea where the file is or how i can link to it or whether the file even uploaded at all? starting to think it just inserted a string. My model below.

class Comment < ActiveRecord::Base
  belongs_to :contact
  belongs_to :company
  mount_uploader :commentfile, CommentFileUploader
end

and comment_file_uploader.rb

class CommentFileUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

Please help!

Also to note if i do

u = Comment.new
u.commentfile = params[:file]

in console i get

NameError: undefined local variable or method `params' for main:Object

The migration adding :commentfile

class CreateUploader < ActiveRecord::Migration
  def self.up
    add_column :comments, :commentfile, :string
  end

  def self.down
  end
end

解决方案

The browser has to use a special format to post the upload file data with the form data. You need to make the form multipart.

<%= form_for( [@company, @company.comments.build],
              :html => { :multipart => true } ) do |f| %>

This adds the attribute enctype="multipart/form-data" to the generated HTML, and the browser should then be able to send the uploaded file in a separate part of the message.

If you use Firebug or similar to examine the post data, you'll see that without the multipart encoding enabled, the browser just doesn't send the file data.

这篇关于载波问题-文件未显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆