I am trying to make an app with Rails 4.
I use simple form for forms.
I am trying to follow this tutorial so that my polymorphic comments model can be used to add comments to articles. https://gorails.com/episodes/comments-with-polymorphic-associations?autoplay=1
I have models for article and comment as follows:
article.rb
has_many :comments, as: :commentable
accepts_nested_attributes_for :comments
comment.rb
belongs_to :user
belongs_to :commentable, :polymorphic => true
The comment controllers have been setup as shown in the video tutorial:
comments_controller.rb
Article/comments_controller.rb
The article controller has:
def new
@article = Article.new
@article.comments.build
end
def article_params
params[:article].permit(:user_id, :body, :title, :image, :tag_list,
comment_attributes: [:opinion])
end
The article show page has:
<div class="col-xs-12">
<%= render :partial => 'comments/form', locals: {commentable: @article} %>
</div>
The comments form partial has:
<%= simple_form_for [commentable, Comment.new] do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :opinion, as: :text, :label => "Add your thoughts", :input_html => {:rows => 4} %>
</div>
<div class="form-actions">
<%= f.button :submit, "Submit", :class => 'formsubmit' %>
</div>
<% end %>
The routes are:
resources :articles do
collection do
get 'search'
end
resources :comments, module: :articles
end
When I save all of this and try to render the articles show page, I get this error:
undefined method `new' for #
The error points to the comments controller create action:
def create
@comment = @commentable.new(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.save
format.html { redirect_to @commentable }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
I don't know what the problem is. I can't understand why this isn't working or what this error message means. I'm wondering if its because comment belongs to both user and commentable.
In fact when I push this and try to see it in production mode, I get a failure and heroku logs show this error:
Exiting
2016-01-02T02:27:59.274318+00:00 app[web.1]: /app/app/controllers/Articles/comments_controller.rb:1:in `<top (required)>': uninitialized constant Articles (NameError)
The entire Article/comments controller has:
class Articles::CommentsController < CommentsController
before_action :set_commentable#, only: [:show, :edit, :update, :destroy]
private
# Use callbacks to share common setup or constraints between actions.
def set_commentable
@commentable = Article.find(params[:article_id])
end
end
So this now works on new articles, but only for 1 comment. If I try and add a second comment to a single article, I get this error:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_comments_on_commentable_type_and_commentable_id" DETAIL: Key (commentable_type, commentable_id)=(Article, 4) already exists. : INSERT INTO "comments" ("opinion", "user_id", "commentable_id", "commentable_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"