Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3795644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:06:55+00:00 2026-05-19T13:06:55+00:00

class CommentsController < ApplicationController def create @commentable= context_object() @comment = @commentable.comments.build(params[:comment].merge(:user_id => current_user.id)) if

  • 0
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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:06:56+00:00Added an answer on May 19, 2026 at 1:06 pm

    Posting this as an answer in order not to clutter the comments to the original question.

    Since you don’t have the requirement to keep attachments available via /attachments – making the second resources block useless, do something like this:

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

    That’s going to change your routes helpers (_path and _url), go through your controller(s) and view(s) and change them to reflect your new helpers.

    Specifically, attachment_comments_path becomes project_album_attachment_comments_path.

    The full list of routes for those models can be viewed by running rake routes in a console. I’d also recommend you take a closer look to the Rails routing guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
class MyBase { protected object PropertyOfBase { get; set; } } class MyType :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.