我已經設置了回形針,並且它與我自己創建的模型正常工作。
我試着用模型做同樣的事情,這個模型是由gem創建的,叫做acts_as_commentable_with_threading 但是它永遠不會保存存儲圖片的file_name。但它照常保存評論。
這真的很奇怪,它只是不會採取回形針文件上傳。如何將paperclip啓用爲acts_as_commentable_with_threading?
爲什麼?
型號/ comment.rb(當然,我已遷移需要評語表列)的意見/用戶/ show.html.erb
attr_accessible :comment_icon
has_attached_file :comment_icon,
:styles => {
:thumb=> "100x100>",
:small => "400x400>" }
<%= render 'comment', :user => @user %>
views/users/_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 %>
UPDATE:
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
模型/ comment.rb(由acts_as_commentable_with_threading自動創建的)
這是Comment模型的一部分。在我的用戶操作中添加評論時,這很重要嗎?
# 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
請你加''UsersController'的add_comment'動作的代碼? –
@AhmadSherif我剛剛做過 – MKK
請在'@ comment.save'後面加上'@ comment.save_attached_files',告訴我會發生什麼。 –