Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Joseph Gaskin
Joseph Gaskin
2,620 Points

Need Help - How do I add comments to my Rails App?

This is my first Rails app. I'm attempting to make a blog with users, posts, and comments. I've already created users and posts, but I'm having some trouble getting my comments form to work. I've already included relationship between the comments and the post, but I'm using nested routes so it's possible I'm just calling something incorrectly.

This is my current error:

No route matches {:action=>"index", :controller=>"comments", :id=>"1", :username=>#}, missing required keys: [:post_id]


This my routes file: routes.rb

Rails.application.routes.draw do
    root 'sessions#new'

    resources :users

    resources :sessions, only: [:new, :create, :destroy]

    scope ':username' do
    resources :posts, except: [:create] do
      resources :comments
      end
    end

    scope ':username' do
    resources :admin, only: [:index]
    end

    post '/:username/posts', to: 'posts#create', as: 'user_posts'
    get 'signup', to: 'users#new', as: 'signup'
    get 'login', to: 'sessions#new', as: 'login'
    get 'logout', to: 'sessions#destroy', as: 'logout'
    get '/:username/admin', to: 'admin#index', as: 'admin'

end

This my Posts Controller file: posts_controller.rb

class PostsController < ApplicationController

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])

  @comment = @post.comments.new
  end

  def new
    @post = Post.new

  end

  def create
    @post = current_user.posts.new(post_params)
    if (@post.save)
      redirect_to posts_path
    else
      render 'new'
    end
  end

  def edit
    @post = Post.find(params[:id])

  end

  def update
    @post = Post.find(params[:id])
    if (@post.update(post_params))
      redirect_to post_path(current_user.username, @post)
    else
      render 'edit'
    end
  end

  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    redirect_to posts_path
  end

  private def post_params
    params.require(:post).permit(:title, :body)
  end
end

This my Comments Controller file: comments_controller.rb

class CommentsController < ApplicationController
  def new
    @comment = Comment.new
  end

  def create
    @post = current_user.post.find(params[:id])
    @comment = @post.comments.build(comment_params)
    redirect_to user_post_path(@post)
  end

  private
  def comment_params
    params.require(:comment).permit(:username, :body)
  end
end

This my comments form: app/views/comments/_form.html.erb

<h3> Add Comment </h3>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <%= f.error_messages %>

  <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>