2

ペーパークリップをセットアップしましたが、自分で作成したモデルでうまく機能しています。私はacts_as_commentable_with_threading
という宝石によって作成されたモデルで同じことをしようとしました が、画像が保存されているfile_nameを保存することはありません。ただし、コメントは通常どおり保存されます。 これは、ペーパー クリップ ファイルのアップロードを必要としないという点で、非常に奇妙です。

なんで?

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 で自動生成)

これは Comment モデルの一部でした。これは、User アクションにコメントを追加するときに問題になりますか????

  # 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.saveに次の行を追加してみてください。add_comment

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