Reputation: 2687
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"
Upvotes: 2
Views: 698
Reputation: 76774
Lots of issues.
This is how it should look:
--
#config/routes.rb
resources :articles do
get :search, on: :collection
resources :comments, module: :articles #-> url.com/articles/:article_id/comments
end
#app/controllers/articles/comments_controller.rb #-> this is the heroku error
class Articles::CommentsController < ApplicationController
before_action :set_article
respond_to :js, :json, :html
def create
@comment = @article.comments.new comment_params
respond_with @comment
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.find params[:article_id]
end
def comment_params
params.require(:comment).permit(:opinion).merge(user: current_user)
end
end
You should be creating comments as their own entity (not as a nested attribute of articles). You should do this using the following:
#app/views/articles/show.html.erb
<%= content_tag :div, render(partial: 'comments/form', locals: { article: @article }), class: "col-xs-12" %>
#app/views/comments/_form.html.erb
<%= simple_form_for [article, article.comments.new] do |f| %>
<%= f.input :opinion, as: :text, label: "Add your thoughts", input_html: {rows: 4} %>
<%= f.submit %>
<% end %>
This should create a standalone comment on the back of the Article
that's been invoked.
Upvotes: 0
Reputation: 2667
Try:
def create
@comment = Comment.new(comment_params)
@comment.user = current_user
@comment.commentable = @commentable
respond_to do |format|
if @comment.save
format.html { redirect_to @comment }
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
Upvotes: 0