Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

khemlall mangal asked in Rails

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

<%= notice %>

Title: <%= @squeal.title %> <%=%>

Body: <%= @squeal.body %>

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

<%= render partial:"comments/comments",locals:{commentable:@squeal} %> <%= render partial:"comments/form",locals:{commentable:@squeal} %>

Comments

<% @squeal.comments.each do |comment|%> <%=comment.body%> <%end%>

_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 %>

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.