11

README には、コントローラーの処理方法と、このプラグインのセットアップの側面を表示する方法は示されていません。数時間検索しましたが、このプラグインの使用方法を示すものは見つかりません。

4

3 に答える 3

18

さらに検索した後、チュートリアルを見つけることをあきらめ、これを思いつきました。誰かがこれを行うためのより良い/よりクリーンな方法を指摘できる場合は、お知らせください。そうでなければ、これが他の誰かに利益をもたらす場合に備えて、私が今使っているものです。

まず、プラグインをインストールしますscript/plugin install http://github.com/jackdempsey/acts_as_commentable.git -r 2.x

次に、コメント モデルと移行を生成しscript/generate comment、データベースを移行しますrake db:migrate

注意が必要なのは、多態的な方法で他のリソースの下にコメントをネストすることです。これが私がしたことです:

# In config/routes.rb
map.resources :comments, :path_prefix => '/:commentable_type/:commentable_id'


# In app/controllers/comments_controller.rb
before_filter :load_commentable
def create
  @comment = @commentable.comments.build(params[:comment])
  @comment.user = current_user
  respond_to do |format|
    if @comment.save
      format.html { redirect_to @commentable }
    else
      format.html { render :action => 'new' }
    end
  end
end

protected
def load_commentable
  @commentable = params[:commentable_type].camelize.constantize.find(params[:commentable_id])
end


# In app/views/comments/_form.html.erb
<%= form_for(:comment, :url => comments_path(commentable.class.to_s.underscore, commentable.id)) do |f| %>


# In app/views/model_that_allows_comments/show.html.erb
<%= render :partial => 'comments/form', :locals => {:commentable => @model_that_allows_comments} %>

何が起こっているのかを理解するのに十分なほど、関連する部分を明確に示していると思います。acts_as_commentableあらゆるモデルへの追加を可能にします。コメント フォームをレンダリングするときに、コメント可能なオブジェクトを locals ハッシュに渡すだけで、同じコメント コントローラー/ビュー コードが機能するはずです。

于 2010-09-18T22:19:40.203 に答える
7

acts_as_commentableコメントモデルを公開し、そのモデルとコメント可能なモデルの間の配管を処理するだけです。コントローラやビューは提供されません。アプリケーションのこの部分をどのように実装するかを決定するのはあなたの責任です。

ただし、これは非常に簡単です。例えば...

# in routes.rb
map.resources :posts, :has_many => :comments

# in your comments controller...
class CommentsController < ApplicationController
  before_filter :get_post

  def get_post
    @post = Post.find(params[:post_id])
  end

  def index
    @comments = @post.comments.all # or sorted by date, or paginated, etc.
  end
end

# In your haml view...
%h1== Comments for #{@post.title}:
%ul
  - comments.each do |comment|
    %h3= comment.title
    %p= comment.comment

ここに移動すると、特定の投稿に対するコメントが表示され/posts/1/commentsます。

于 2010-09-18T13:25:31.593 に答える
2

モデルにコメントを追加する最良の方法は、このように ApplicationController.rb ファイルに comment というメソッドを作成することだと思います。

def comment 
  # Extracts the name of the class
  klass = self.class.to_s[/\A(\w+)sController\Z/,1] 
  # Evaluates the class and gets the current object of that class
  @comentable_class = eval "#{klass}.find(params[:id])"
  # Creates a comment using the data of the form
  comment = Comment.new(params[:comment])
  # Adds the comment to that instance of the class
  @comentable_class.add_comment(comment)

  flash[:notice] = "Your comment has been added!"
  redirect_to :action => "show", :id => params[:id] 
end    

次に、部分的な_comment.rbを作成して、必要なモデルで使用します

<%= form_tag :action => "comment", :id => Your_model_goes_here %>
  <p><label for="comment_title">Title</label><br/>
  <%= text_field 'comment', 'title' %></p>
  <%= text_area "comment", "comment", :rows => 5, :cols => 50 %> <br />
  <%= submit_tag "Comment!.." %>
</form>

誰かの役に立てば幸いです...

于 2011-09-15T19:39:26.210 に答える