Here is my form for my comments _form.html.erb:
<%= form_for([post, @comment]) do |f| %>
<p>
<%= f.text_area :body, placeholder: "Write a comment!" %>
</p>
<br>
<p> <%= f.submit %> </p>
<% end %>
and here is my posts form _form.html.erb:
<%= form_for @post, url: post_path(@post), method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :Edit_Post %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Here is my comments_controller.rb:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params[:comment])
@comment.save
redirect_to @posts
end
def destroy
@user = User.find(session[:user_id])
@posts = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
end
And here is my posts_controller.rb:
class PostsController < ApplicationController
before_action :load_post, only: [:show, :edit, :update, :destroy]
def index
@user = User.find(session[:user_id])
@posts = Post.all
end
def welcome
@user = User.find(session[:user_id])
@posts = Post.order("created_at desc").limit(4).offset(1)
@signed_in_user = session[:user_id]
end
def posts
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
@signed_in_user = session[:user_id]
end
def new
@post = Post.new
@user = User.find(session[:user_id])
end
def create
@user = User.find(session[:user_id])
@post = Post.new(post_params)
@post.user_id = @signed_in_user
if @post.save
redirect_to dashboard_path
else
render 'new'
end
end
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
end
def edit
@user = User.find(session[:user_id])
end
def update
if @post.update(post_params)
redirect_to @post, notice: "Your post has been updated!"
end
end
def destroy
@user = User.find(session[:user_id])
@post.user_id = @signed_in_user
@post.destroy
redirect_to posts_path
end
private
def load_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:body)
end
end
Having this issue. Any help will be appreciated. Here is the picture of the error I am having:
EDIT: Here is my log
Started GET "/posts/24" for 127.0.0.1 at 2015-09-28 18:46:44 -0700
Processing by PostsController#show as HTML
Parameters: {"id"=>"24"}
[1m[36mPost Load (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", 24]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1[0m [["id", "24"]]
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 24]]
Rendered comments/_form.html.erb (1.0ms)
Rendered posts/show.html.erb within layouts/application (7.0ms)
Rendered layouts/_nav.html.erb (1.0ms)
Completed 200 OK in 451ms (Views: 412.0ms | ActiveRecord: 1.0ms)
undefined local variable or method `post'
Your error message says it all. It tells you that, you don't have post
defined in the corresponding controller action for your view. That's why you are getting that error.
So, you need to define @post
(usually you use a instance variable in such cases) in the corresponding controller action i.e. PostsController
's show
action. You actually need to define @comment
as well as you don't have it in your controller action currently. So, update your show
method like this:
def show
@user = User.find(session[:user_id])
@signed_in_user = session[:user_id]
# you need to define @post and @comment
@post = Post.find params[:id]
@comment = Comment.new(post: @post)
end
Then, you can use @post
and @comment
in your view, so your comments/_form.html.erb
would look like this:
<%= form_for([@post, @comment]) do |f| %>
. . .
. . .