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?
I think you have to options:
Option 1)
The view:
The controller:
Option 2)
In the controller:
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.
And then in your controller you could do:
And in the new view:
So the create action could look something like:
Maybe there are even better ways to do it…