Rails 3 - Helping with a Commenting Module -


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 |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 ran hitch morning, possibly because of use of nested resources. essentially, have url like:

/projects/3/albums/6/attachments/84 

when comment on page, error:

activerecord::recordnotfound (couldn't find project without 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   resources : albums     resources :attachments   end end  resources :attachments     resources :comments, :only => [:create, :update,:destroy],               :constraint => {:context_type => "conversations"} end 

any ideas on how can commenting module play nicely commenting on project>album>attachment ?

thanks input,

posting answer in order not clutter comments original question.

since don't have requirement keep attachments available via /attachments - making second resources block useless, this:

resources :projects   resources :albums     resources :attachments       resources :comments, :only => [:create, :update,:destroy],                            :constraint => {:context_type => "conversations"}     end   end end 

that's going change routes helpers (_path , _url), go through controller(s) , view(s) , change them reflect new helpers.

specifically, attachment_comments_path becomes project_album_attachment_comments_path.

the full list of routes models can viewed running rake routes in console. i'd recommend take closer rails routing guide.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -