2

コメントコントローラーでRailsAttached_resourcegemを使用していますが、コメントはネストされたリソースであるため、
resources:projects do
  resources:comments do
end

コメントコントローラーにもbelongs_toがあります:
belongs_to:project、:finder =>:find_by_project_uuid!、:class_name => "Thfz :: Project"、:polymorphic => true

  1. コメントの作成時に、コメントのユーザー関連付けをcurrent_user(user_id)に設定するにはどうすればよいですか?user_idは大量に割り当てられるとは想定されていないため。

  2. 次のことを試しまし た
    。defbegin_of_association_chaincurrent_userendこれにより、ユーザーIDが正しく設定され
         ます が、これを使用してプロジェクトでネストされたリソースを機能させることはできません。

  3. コメントを破棄するときにも同じ質問があります。current_userからコメントを見つける必要があります。これを実現するにはどうすればよいですか?

では、独自の作成アクションと破棄アクションを作成する必要がありますか?

ありがとう :)

4

1 に答える 1

2

次のcomments_controller内を試しましたか?

class CommentsController < InheritedResources::Base
  before_filter :authenticate_user! # Assuming you are using Devise for authentication
  respond_to :html, :xml, :json

  belongs_to :project, :finder => :find_by_project_uuid!, :class_name => "Thfz::Project"

  def create
    @comment = build_resource
    @comment.author = current_user

    create!
  end
end
于 2013-01-23T12:56:21.993 に答える