Notifications
You’re not receiving notifications from this thread.
how do i link a comment to a specific post...
Can someone give me an example of POST and Comment.. i did a scaffold of a blog for example but i am not sure how to integrate the part where you can link a comment to a specific post....
COMMENTS controller
class CommentonsquealsController < ApplicationController
before_action :set_commentonsqueal, only: [:show, :edit, :update, :destroy]
# GET /commentonsqueals
# GET /commentonsqueals.json
def index
@commentonsqueals = Commentonsqueal.all
@squeal = Squeal.all
end
# GET /commentonsqueals/1
# GET /commentonsqueals/1.json
def show
end
# GET /commentonsqueals/new
def new
@commentonsqueal = Commentonsqueal.new(Squeal.find_by_id(params[:id]))
end
# GET /commentonsqueals/1/edit
def edit
end
# POST /commentonsqueals
# POST /commentonsqueals.json
def create
@commentonsqueal = Commentonsqueal.new(commentonsqueal_params)
respond_to do |format|
if @commentonsqueal.save
format.html { redirect_to @commentonsqueal, notice: 'Commentonsqueal was successfully created.' }
format.json { render :show, status: :created, location: @commentonsqueal }
else
format.html { render :new }
format.json { render json: @commentonsqueal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /commentonsqueals/1
# PATCH/PUT /commentonsqueals/1.json
def update
respond_to do |format|
if @commentonsqueal.update(commentonsqueal_params)
format.html { redirect_to @commentonsqueal, notice: 'Commentonsqueal was successfully updated.' }
format.json { render :show, status: :ok, location: @commentonsqueal }
else
format.html { render :edit }
format.json { render json: @commentonsqueal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /commentonsqueals/1
# DELETE /commentonsqueals/1.json
def destroy
@commentonsqueal.destroy
respond_to do |format|
format.html { redirect_to commentonsqueals_url, notice: 'Commentonsqueal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_commentonsqueal
@commentonsqueal = Commentonsqueal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def commentonsqueal_params
params.require(:commentonsqueal).permit(:squeal_id, :body)
end
end
class SquealsController < ApplicationController
before_action :set_squeal, only: [:show, :edit, :update, :destroy]
# GET /squeals
# GET /squeals.json
def index
@squeals = Squeal.all
@commentonsqueals = Commentonsqueal.all
end
# GET /squeals/1
# GET /squeals/1.json
def show
@comments = Commentonsqueal.all.where("squeal_id = ?", Squeal.find_by_id(params[:id]))
end
# GET /squeals/new
def new
@squeal = Squeal.new
end
# GET /squeals/1/edit
def edit
end
# POST /squeals
# POST /squeals.json
def create
@squeal = Squeal.new(squeal_params)
respond_to do |format|
if @squeal.save
format.html { redirect_to @squeal, notice: 'Squeal was successfully created.' }
format.json { render :show, status: :created, location: @squeal }
else
format.html { render :new }
format.json { render json: @squeal.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /squeals/1
# PATCH/PUT /squeals/1.json
def update
respond_to do |format|
if @squeal.update(squeal_params)
format.html { redirect_to @squeal, notice: 'Squeal was successfully updated.' }
format.json { render :show, status: :ok, location: @squeal }
else
format.html { render :edit }
format.json { render json: @squeal.errors, status: :unprocessable_entity }
end
end
end
# DELETE /squeals/1
# DELETE /squeals/1.json
def destroy
@squeal.destroy
respond_to do |format|
format.html { redirect_to squeals_url, notice: 'Squeal was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_squeal
@squeal = Squeal.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def squeal_params
params.require(:squeal).permit(:title, :body)
end
end
Not 100% sure what you're asking, but you want to add comments to a post? Not sure what you have apart from the scaffolded Controllers.
Post.rb
has_many :comments
Comment.rb
belongs_to :post
Be sure your comment model has post_id
. From there you can try in in the rails console rails console
, like so: Post.first.comments.create(body: 'My first comment to the first Post')
.
For more in depth, check this episode as well:
https://gorails.com/episodes/comments-with-polymorphic-associations?autoplay=1
Hi Thank you, i followed this tutorial however i am getting
Routing Error
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 %>
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 %>
Really hard to read the code this way. Could you try to clean it up? Views can removed.
Also: check the log, check the error you have uninitialized constant Squeals
and on which line and file this is. That part of the code might be worth to post here.
Thank you Jack..
Here is the Problem, when i click on Submit for a comment, i get uninitialized constants Squeals.
on my Squeal/Show.html.erb file where i show a specific or selected post
Url: https://squealing-app-squeldev.c9users.io/squeals/6
i enter my comments and clink on submit and then i am taking to URL https://squealing-app-squeldev.c9users.io/squeals/6/comments
With Error
uninitialized constant Squeals
Rails.root: /home/ubuntu/workspace
Application Trace | Framework Trace | Full Trace
Routes
SHOW.HTML.ERB FILE
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @squeal.title %>
<%=%>
</p>
<p>
<strong>Body:</strong>
<%= @squeal.body %>
</p>
<%= link_to 'Edit', edit_squeal_path(@squeal) %> |
<%= link_to 'Back', squeals_path %>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="well">
<%= render partial:"comments/comments",locals:{commentable:@squeal} %>
<%= render partial:"comments/form",locals:{commentable:@squeal} %>
<h2>Comments</h2>
<% @squeal.comments.each do |comment|%>
<%=comment.body%>
<%end%>
</div>
</div>
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
file: squeal/comments_controller
class Squeals::CommentsController <CommentsController
before_action :set_commentable
private
def set_commentable
@commentable = Squeal.find(params[:squeal_id])
end
end