class CommentsController < ApplicationController
def create
@commentable= context_object() @comment = @commentable.comments.build(params[:comment].merge(:user_id => current_user.id))
if @comment.save respond_to do |format| format.js end else render :action => 'new' end end
private
def context_object params[:constraint] [:context_type].singularize.classify.constantize.find( context_id ) end
def context_id params["#{ params[:constraint][:context_type].singularize } _id"] end
end
This commenting module has served me well but I ran into a hitch this morning, possibly because of my use of nested resources. Essentially, I now have a URL like:
/projects/3/albums/6/attachments/84
When I comment on that page, I get the error:
ActiveRecord::RecordNotFound (Couldn't find Project without an ID): app/controllers/comments_controller.rb:102:in `context_object' app/controllers/comments_controller.rb:14:in `create'
My routes file looks like:
resources :projects do resources : albums do resources :attachments end end
resources :attachments do resources :comments, :only => [:create, :update,:destroy], :constraint => {:context_type => "conversations"} end
Any ideas on how I can get the commenting module to play nicely with commenting on `project>Album>Attachment ?`
Thanks for the input,