2

我已经设置了回形针,它与我自己创建的模型配合得很好。
我尝试对模型执行相同的操作,该模型由名为acts_as_commentable_with_threading的gem 创建, 但是它从不保存存储图片的文件名。但它像往常一样保存评论。
这真的很奇怪,它不会上传回形针文件。

为什么?

models/comment.rb(当然,我已经迁移了 Comment 表中的必需列)

attr_accessible :comment_icon

has_attached_file :comment_icon,
    :styles => {
    :thumb=> "100x100>",
    :small  => "400x400>" } 

意见/用户/show.html.erb

<%= render 'comment', :user => @user %>

意见/用户/_comment.html.erb

<%=form_for :users, url: url_for( :controller => :users, :action => :add_comment ) do |f| %>

    <div class="field">
      <%= f.label :body %><br />
      <%= f.text_field :body %>
    </div>
    <div class="field">
    <%= f.file_field :comment_icon %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

更新:

users_controller.rb

def add_comment
    @user = User.find_by_username(params[:id])
    @user_who_commented = current_user
    @comment = Comment.build_from( @user, @user_who_commented.id, params[:users][:body] )
    @comment.save
    redirect_to :controller => 'users', :action => 'show', :id => @user.username
    flash[:notice] = "comment added!"
end




def delete_comment
    @comment = Comment.find(params[:id])

    if current_user.id == @comment.user_id
        @comment.destroy
        flash[:notice] = "Deleted!"
    else
        flash[:notice] = "Sorry, you can't delete this comment"
    end
    redirect_to :controller => 'users', :action => 'show', :id => params[:username]
end

models/comment.rb(由acts_as_commentable_with_threading自动创建)

这是评论模型的一部分。当它在我的用户操作中添加评论时,这是否重要????

  # Helper class method that allows you to build a comment
  # by passing a commentable object, a user_id, and comment text
  # example in readme
  def self.build_from(obj, user_id, comment)
    c = self.new
    c.commentable_id = obj.id
    c.commentable_type = obj.class.base_class.name
    c.body = comment
    c.user_id = user_id
    c
  end
4

1 回答 1

1

尝试@comment.saveadd_comment实际操作之前添加此行:

@comment.comment_icon = params[:users][:comment_icon]
于 2012-12-25T18:28:30.640 回答