Question

I am using Rails Inherited_resource gem in my comments controller, and comments is a nested resource so:
resources :projects do
  resources :comments do
end

I also have a belongs_to in the comments controller:
belongs_to :project, :finder => :find_by_project_uuid!, :class_name => "Thfz::Project", :polymorphic => true

  1. How can I set the comment's user association to the current_user(user_id) when its created? As user_id is not suppose to be massive assigned.

  2. I tried following:
    def begin_of_association_chain
         current_user
    end
    This does set the user id correctly, but I cannot get nested resource working for Project with this.

  3. Same question come when destroy a comment, I will need to find the comment through current_user, so how to achieve this?

So do I have to write my own create and destroy actions?

Thanks :)

Was it helpful?

Solution

Have you tried the following inside 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top