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

  • Home
  • SEARCH
  • 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 1087131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:53:59+00:00 2026-05-16T22:53:59+00:00

I’m working to install the acts_as_commentable plugin on my Rails 3 app. After adding

  • 0

I’m working to install the acts_as_commentable plugin on my
Rails 3 app.

After adding “acts_as_commentable” to my book model, I then added a
comment form on my book show view:

<% form_for(@comment) do|f| %> 
  <%= f.hidden_field :book_id %> 
  <%= f.label :comment %><br /> 
  <%= f.text_area :comment %> 
  <%= f.submit "Post Comment" %> 
<% end %> 

Then in the controller (comments_controller.rb),

  def create 
    @comment = Comment.new(params[:comment]) 
    Book.comments.create(:title => "First comment.", :comment => "This 
is the first comment.") 
  end 

Then when submitting a comment, it returns the error: “unknown
attribute: book_id”
From the log:

  Processing by CommentsController#create as HTML 
  Parameters: {"comment"=>{"comment"=>"WOOOW", "book_id"=>"32"}, 
"commit"=>"Post Comment", 
"authenticity_token"=>"5YbtEMpoQL1e9coAIJBOm0WD55vB2XRZMJa4MMAR1YI=", 
"utf8"=>"✓"} 
Completed   in 11ms 
ActiveRecord::UnknownAttributeError (unknown attribute: book_id): 
  app/controllers/comments_controller.rb:3:in `new' 
  app/controllers/comments_controller.rb:3:in `create' 

Suggestions?

  • 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-16T22:54:00+00:00Added an answer on May 16, 2026 at 10:54 pm

    I think you have to options:

    Option 1)
    The view:

    <% form_for(@comment) do|f| %> 
      <%= f.hidden_field :commentable_id, :value => @book.id %>
      <%= f.hidden_field :commentable_type, :value => 'Book' %> 
      <%= f.label :comment %><br /> 
      <%= f.text_area :comment %> 
      <%= f.submit "Post Comment" %> 
    <% end %> 
    

    The controller:

    def create
      @comment = Comment.create(params[:comment])
    end
    

    Option 2)

    In the controller:

    def create
      @book = Book.find(params[:comment][:book_id])
      @comment = @book.comments.create(params[:comment].except([:comment][:book_id]))
    end
    

    I haven’t tested the code, but the idea should be correct

    Update, given you want to comment on various models (i’m writting my code without testing it…). So let’s say you have Book and Magazine, and you want to comment on them. I guess I would define nested routes for them.

    map.resources :books, :has_many => :comments
    map.resources :magazines, :has_many => :comments
    

    And then in your controller you could do:

    before_filter :find_commentable
    
    def find_commentable
      @commentable = Book.find(params[:book_id]) if params[:book_id]
      @commentable = Magazine.find(params[:magazine_id]) if params[:magazine_id]
    end
    

    And in the new view:

    <% form_for :comment, [@commentable, @comment] do |f| %>
      <%= f.label :comment %>
      <%= f.text_area :comment %>
      <%= f.submit %>
    <% end %>
    

    So the create action could look something like:

    def create
      @user.comments.create(params[:comment].merge(:commentable_id => @commentable.id, :commentable_type => @commentable.class.name))
      redirect_to @commentable
    end
    

    Maybe there are even better ways to do it…

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

Sidebar

Related Questions

No related questions found

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.