Skip to main content

routing errors when i try to submit a comment to a post

Rails • Asked by khemlall mangal

Hi Thank you, i followed this tutorial however i am getting
Routing Error when i click submit for the comment...

uninitialized constant Squeals

Rails.root: /home/ubuntu/workspace

Application Trace | Framework Trace | Full Trace

here is my routes.rb:

Rails.application.routes.draw do

  resources :commentonsqueals
  resources :squeals do
    resources :comments, module: :squeals

end

  get 'squeals/index'
   resources :campaign
  devise_for :users
  resources :users do
    get "users/show_image" => "users#show_image"
    member do
      get :following, :followers
    end
  end
  resources :relationships, only: [:create, :destroy]
  resources :posts, only:[:create,:destroy]
  # Define Root URL
  root 'pages#index'

  # Define Routes for Pages
  get '/home' => 'pages#home' # override default routes.
  get '/user/:id' => 'pages#profile'
  get '/explore' => 'pages#explore'
 put 'posts/:id', to: 'posts#update'
get 'posts/:id/edit', to: 'posts#edit', as: :edit_post
get '/signup' => 'pages#signup'
  get '/show' => 'pages#show'
    get 'show/:id' => 'pages#show'

   get '/campaign' => 'campaign#index'
    get 'dashboard'=>'pages#dashboard'

squeal RB file

```
class Squeal < ActiveRecord::Base
has_many :comments, as: :commentable

end

COMMENTS.RB FILE

class Comment < ActiveRecord::Base
belongs_to:commentable, polymorphic:true
end

Comments-> Comments.html.erb
Comments-> _form.html.erb

squeal->comments_controller

class Squeals::CommentsController <CommentsController
before_action :set_commentable

private
def set_commentable

@commentable = Squeal.find(params[:id])

end

end

**comments controller**

class CommentsController < ApplicationController
before_action:authenticate_user!

def create
@comment = @commentable.comments.new comment_params
@user.user = current_user
comment.save
redirect_to @commentable, notice: "Your comment was posted"
end

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

end

SQEALS-> show.html.erb

Title:

Body:

<%= link_to 'Edit', edit_squeal_path(@squeal) %> |
<%= link_to 'Back', squeals_path %>

Comments

_form.html.erb

<%= form_for(@squeal) do |f| %>

<% if @squeal.errors.any? %>


<%= pluralize(@squeal.errors.count, "error") %> prohibited this squeal from being saved:

  <ul>
  <% @squeal.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
</div>

<% end %>


<%= f.label :title %>

<%= f.text_field :title %>


<%= f.label :body %>

<%= f.text_area :body %>


<%= f.submit %>

<% end %>

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.