2011-08-18 80 views
0
<% @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 %> 

是我想显示从下面的表格上传的文件:载波问题 - 文件没有显示?

<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 %> 

,但我不知道该文件是否被保存东阳当我检查我的公共/上传文件夹中没有文件出现。在<%= comment.commentfile %>的视图中,我得到了我上传的文件的名称,但不知道文件的位置或我如何链接到该文件,或者文件是否甚至上传了?开始认为它只是插入了一个字符串。我的模特如下。

class Comment < ActiveRecord::Base 
    belongs_to :contact 
    belongs_to :company 
    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}/#{mounted_as}/#{model.id}" 
    end 
end 

请帮帮忙!

另外需要注意的,如果我做

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

在控制台我得到

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

迁移补充说:commentfile

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

    def self.down 
    end 
end 
+0

当您提交表单时,日志中的任何内容? – Chowlett

回答

1

浏览器必须使用特殊的格式发布用表格数据上传文件数据。您需要将表单分成多个部分。

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

这增加了属性ENCTYPE =“多部分/格式数据”,以将所生成的HTML,然后浏览器应该能够发送上载文件中的消息的一个独立部分。

如果您使用Firebug或类似方式检查发布数据,您会发现如果未启用多部分编码,浏览器将不会发送文件数据。