我已经设置了回形针,它与我自己创建的模型一起正常工作 我试图用模型做同样的事情,模型由名为 acts_as_commentable_with_threading 的gem创建 但是它永远不会保存存储图片的file_name。但它像往常一样保存评论 这真是奇怪,它只是不会采取纸夹文件上传。
为什么?
models / comment.rb (当然,我已在评论表中迁移了必需的列)
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
答案 0 :(得分:1)
尝试在@comment.save
操作中add_comment
之前添加此行:
@comment.comment_icon = params[:users][:comment_icon]