Apologies for the newbie question but, I'm getting the following error referencing message#new action in my Messages controller:
ActiveRecord::RecordNotFound (Couldn't find User without an ID): app/controllers/messages_controller.rb:18:in `new'
#recipient = User.find(params[:user_id])
I'm trying to incorporate the ancestry gem in my messages_controller, specifically, when I try to reply to a message that has been received in the user inbox(index.html.erb). I understand that there is currently no ID being passed, but does anyone encountered a similar issue? Code below:
class MessagesController < ApplicationController
def index
#messages = current_user.to_messages
end
def outbox
type = (params[:type] && params[:type] == "sent" ) ? "from_messages" : "to_messages"
#messages = current_user.from_messages
end
def show
#message = Message.find params[:id]
end
def new
#message = Message.new(:parent_id => params[:parent_id])
#recipient = User.find(params[:user_id])
end
def create
#message = Message.new message_params
#message.sender_id = current_user.id
if #message.save
flash[:success] = "Your message has been sent!"
redirect_to users_path
else
flash[:failure] = "Please try again."
redirect_to users_path
end
end
def destroy
#message = Message.find(params[:id])
#message.destroy
redirect_to messages_path
end
private
def message_params
params.require(:message).permit(:content, :sender_id, :recipient_id, :parent_id)
end
end
show.html.erb (view)
From: <%= link_to #message.recipient.first_name + " " + #message.recipient.last_name, user_path(#message.recipient.id) %>,
To: <%= #message.sender.first_name + " " + #message.sender.last_name %>,
Message: <%= #message.content %>
<% if #message.recipient_id == current_user.id %>
<%= link_to "Reply", new_message_path(:parent_id => #message) %>
<% end %>
Started GET "/messages/new?parent_id=19" for 127.0.0.1 at 2014-09-27 15:41:18 -0400
Processing by MessagesController#new as HTML
Parameters: {"parent_id"=>"19"}
Message Load (0.1ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT 1 [["id", 19]]
Completed 404 Not Found in 11ms
ActiveRecord::RecordNotFound (Couldn't find User without an ID):
app/controllers/messages_controller.rb:18:in `new'
Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /Users/sikendersingh/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.8ms)
I'm not sure about the recipient/sender issue, but I think you just need to pass user_id as a param from your view. So the link_to would become: <%= link_to "Reply", new_message_path(:parent_id => #message, :user_id => #message.recipient_id) %>. This will make it available in the new action.
Related
I am following a guide to build a blog with rails, simple Post model with title and body.
I am using simple form and upon form submission to create a new post, the post saves created_at and updated_at values, but not the actual content submitted in the form.
I have attempted removing the code for simple form and using Rails native form_for. This DOES save all values to the database. I am new to simple form, not certain whether or not I am using it correctly.
Here is the console record:
Started POST "/posts" for ::1 at 2019-08-17 13:51:01 -0500
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qY8kYZxVIMBL8lzHYuQ4qOu6nXsTGLWCRhLPJ2eiAU8EyzR61fZppAFBYmgcm3rx02FYAHcCgFBVlUyDTLtDGA==", "post"=>{"title"=>"Simple Form Test", "body"=>"<p>Test Test Test</p>\r\n"}, "commit"=>"Create Post"}
(0.0ms) begin transaction
SQL (3.3ms) INSERT INTO "posts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2019-08-17 18:51:01.325736"], ["updated_at", "2019- 08-17 18:51:01.325736"]]
(7.7ms) commit transaction
Redirected to http://localhost:3000/posts/3
Completed 302 Found in 28ms (ActiveRecord: 11.1ms)
Here is the form:
<%= simple_form_for #post do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2>
<%= "#{pluralize(#post.errors.count, "error")} prohibited this post from being saved:" %>
</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.input :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control" %>
</div>
<div class="form-group">
<%= f.button :submit %>
</div>
<% end %>
Here is the controller:
class PostsController < ApplicationController
before_action :find_post, only: [:edit, :update, :show, :delete]
# Index action to render all posts
def index
#posts = Post.all
end
# New action for creating post
def new
#post = Post.new
end
# Create action saves the post into database
def create
#post = Post.new
if #post.save(post_params)
flash[:notice] = "Successfully created post!"
redirect_to post_path(#post)
else
flash[:alert] = "Error creating new post!"
render :new
end
end
# Edit action retrives the post and renders the edit page
def edit
end
# Update action updates the post with the new information
def update
if #post.update_attributes(post_params)
flash[:notice] = "Successfully updated post!"
redirect_to post_path(#post)
else
flash[:alert] = "Error updating post!"
render :edit
end
end
# The show action renders the individual post after retrieving the the id
def show
end
# The destroy action removes the post permanently from the database
def destroy
if #post.destroy
flash[:notice] = "Successfully deleted post!"
redirect_to posts_path
else
flash[:alert] = "Error updating post!"
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
def find_post
#post = Post.find(params[:id])
end
end
Hopin
g to be able to create posts with body and title, and learn more about simple form.
Thanks in advance!
You wrote #post = Post.new without pass your parameters to your object, so when you save you object you save an empty object.
It should be either :
#post = Post.new(post_params)
Or directly
#post = Post.create(post_params)
I'm stuck trying to get my form with a collection_select to save. I have 3 models (Events, Users, Items), plus a joining model to link Items to Events (Event_Items).
My goal is for each users to be able to join events, and each user can specify a list of items they will bring to the event from an inventory that is unique to each user.
My form partial with collection select looks like this:
<%= form_for [#event, #event_items], remote: true do |f| %>
<%= f.hidden_field :user_id, value: current_user.id %>
<div class="form-group">
<%= f.collection_select(:item_id, current_user.items.all, :id, :title, { :prompt => "Select an Item", :selected => #item_id}, { class: "form-control"}) %>
</div>
<%= f.submit "Submit", class: "btn btn-primary", id: "event-item-button" %>
My event_items_controller looks like:
class EventItemsController < ApplicationController
before_action :authenticate_user!
def new
#event_item = EventItem.new
end
def create
#event_item = EventItem.new
#event = Event.find(params[:id])
if #event_item.save
flash[:success] = "Item Added to List"
redirect_to #event
else
flash.now[:danger] = "Please try again"
redirect_to #event
end
end
def destroy
#event_item = EventItem.find(params[:id])
end
private
def event_item_params
params.require(:event_item).permit(:event_id, :user_id, :item_id)
end
end
In the Event show page, I have this section where I iterate over each attendee and render the Event_Item partials:
<% #event.attendees.each do |user| %>
<div class="col-md-4">
#should render each existing item on the list.
#Doesn't work yet
<%= render 'event_items/event_item' %>
</div>
<% if user == current_user %>
#renders dropdown with only current user's inventory
#currently shows items correctly, just doesn't save to
#database
<%= render 'event_items/form' %>
<% end %>
<% end %>
Whenever I hit the Submit button after selecting an Item in the dropdown, nothing is saved to the database, although the server console shows:
Started POST "/Events/27/event_items" for 127.0.0.1 at 2018-09-26 19:54:32 -0500
Processing by EventItemsController#create as JS
Parameters: {"utf8"=>"✓", "event_item"=>{"user_id"=>"5", "item_id"=>"2"}, "commit"=>"Submit", "event_id"=>"27"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 5], ["LIMIT", 1]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.3ms)
ActiveRecord::RecordNotFound (Couldn't find Event without an ID):
app/controllers/event_items_controller.rb:11:in `create'
In your create action, you do:
#event = Event.find(params[:id])
But, params doesn't have :id, it has :event_id. So, it should probably be:
#event = Event.find(params[:event_id])
And, your create action should probably look more like:
def create
#event = Event.find(params[:event_id])
#event_item = #event.event_items.new(event_item_params)
if #event_item.save
flash[:success] = "Item Added to List"
redirect_to #event
else
flash.now[:danger] = "Please try again"
redirect_to #event
end
end
By doing:
#event_item = #event.event_items.new(event_item_params)
event_id will be set on the new event_item. (This assumes, naturally, that Event has_many :event_items and EventItem belongs_to :event.)
I'm building an Events app and I'm trying to create a link from the Event show page to the event creator's profile but I'm getting the following error -
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=21
The error highlights this particular line of code in the Users Controller -
def show
#user = User.find(params[:id])
end
The development log produces this output -
Started GET "/users/21" for ::1 at 2016-04-15 12:37:08 +0100
Processing by UsersController#show as HTML
Parameters: {"id"=>"21"}
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]]
Completed 404 Not Found in 14ms (ActiveRecord: 0.9ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=21):
app/controllers/users_controller.rb:14:in `show'
The user id (in this instance 5) is not being passed.I've tried numerous arguments in the show.html.erb page but none will work. Changing the show argument in the users controller to #user = current_user only succeeds in bringing up the profile of the user viewing the event and not the profile of the event creator.
Here's my code -
Events Controller
class EventsController < ApplicationController
before_action :find_event, only: [:show, :edit, :update, :destroy,]
# the before_actions will take care of finding the correct event for us
# this ties in with the private method below
before_action :authenticate_user!, except: [:index, :show]
# this ensures only users who are signed in can alter an event
def index
if params[:category].blank?
#events = Event.all.order("created_at DESC")
else
#category_id = Category.find_by(name: params[:category]).id
#events = Event.where(category_id: #category_id).order("created_at DESC")
end
# The above code = If there's no category found then all the events are listed
# If there is then it will show the EVENTS under each category only
end
def show
end
def new
#event = current_user.events.build
# this now builds out from a user once devise gem is added
# after initially having an argument of Event.new
# this assigns events to users
end
# both update and create actions below use event_params as their argument with an if/else statement
def create
#event = current_user.events.build(event_params)
# as above this now assigns events to users
# rather than Event.new
if #event.save
redirect_to #event, notice: "Congratulations, you have successfully created a new event."
else
render 'new'
end
end
def edit
# edit form
# #edit = Edit.find(params[:id])
#event = current_user.events.find(params[:id])
end
def update
if #event.update(event_params)
redirect_to #event, notice: "Event was successfully updated!"
else
render 'edit'
end
end
def destroy
#event.destroy
redirect_to root_path
end
private
def event_params
params.require(:event).permit(:title, :location, :date, :time, :description, :number_of_spaces, :is_free, :price, :organised_by, :organiser_profile, :url, :image, :category_id)
# category_id added at the end to ensure this is assigned to each new event created
end
def find_event
#event = Event.find(params[:id])
end
end
Users Controller -
class UsersController < ApplicationController
before_action :authenticate_user!
def new
#user = User.new
end
def show
#user = User.find(params[:id])
end
def create
#user = User.new(user_params)
if #user.save
flash[:success] = "Welcome to Mama Knows Best"
session[:uid] = #user.id
redirect_to root_path
else
render 'new'
end
end
def edit
#user = current_user
end
def update
#user = current_user
if #user.update(user_params)
flash[:success] = "Profile successfully updated!"
redirect_to root_path
else
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :username, :biography, :email, :url)
end
end
Show page -
<%= image_tag #event.image.url %>
<h1><%= #event.title %></h1>
<p>Location </p>
<p><%= #event.location %></p>
<p>Date</p>
<p><%= #event.date.strftime('%A, %d %b %Y') %></p>
<p>Time</p>
<p><%= #event.time.strftime('%l:%M %p') %></p>
<!-- above expresses date and time as per UK expectations -->
<p>More details</p>
<p><%= #event.description %></p>
<p>Number of Spaces available</p>
<p><%= #event.number_of_spaces %></p>
<% if #event.is_free? %>
<p>This is a free event</p>
<% else %>
<p>Cost per person</p>
<p><%= #event.price %></p>
<% end %>
<p>Organiser</p>
<p><%= #event.organised_by %></p>
<p>Organiser Profile</p>
<button><%= link_to "Profile", user_path %></button>
<p>Link to Organiser site</p>
<button><%= link_to "Organiser site", #event.url %></button>
<p>Submitted by</p>
<p><%= #event.user.name %></p>
<% if user_signed_in? and current_user == #event.user %>
<%= link_to "Edit", edit_event_path %>
<%= link_to "Delete", event_path, method: :delete, data: { confirm: "Are you sure?"} %>
<%= link_to "Back", root_path %>
<% else %>
<%= link_to "Back", root_path %>
<%= link_to "Book the Event", new_event_booking_path(#event) %>
<% end %>
routes -
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
resources :users
resources :events do
resources :bookings
end
# get 'welcome/index'
authenticated :user do
root 'events#index', as: "authenticated_root"
end
root 'welcome#index'
# the above method comes from devise and allows for the site to have a home page
# for users not signed in and one for when they are signed in
end
I haven't added anything relating to the users profile on the form partial as I didn't believe it to be relevant. Any help would be much appreciated.
To reiterate your question, you want a link on the event page that goes to the event organiser's profile page?
<p>Organiser Profile</p>
<button><%= link_to "Profile", user_path(#event.user) %></button>
user_path is a path helper in Rails which resolves to RESTful route of /users/:id. This goes in UserController#show and expects params hash to contain :id.
For your case, you are missing the argument. You need to do:
<button><%= link_to "Profile", user_path(current_user) %></button>
It automatically picks up id and passes it to params hash as : {:id => 7}
Doc
You may also want fix other such helpers call:
event_path
edit_event_path with appropriate argument.
What are you using for user authentication, devise or similar gem? Did you build your own? If so do you have current_user defined in the sessions helper? The below code is how current_user could be defined (a la Hartl Rails tutorial). This will allow you to use current_user in views and controllers.
def current_user
if (user_id = session[:user_id])
#current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
#current_user = user
end
end
end
I also noticed in your Users Controller under def create. I believe it should be session[:id] instead of session[:uid]. Please excuse me if this is not the case. Hope this helps.
I am new to rails. I have defined controller for the index of shop_products as follows
shop_profile.rb
class ShopProfile < ActiveRecord::Base
has_and_belongs_to_many :users
has_one :shop_inventory_detail
end
shop_product.rb
class ShopProduct < ActiveRecord::Base
belongs_to :shop_profile
end
shop_products_controller.rb
class ShopProductsController < ApplicationController
def index
#shop_profile = ShopProfile.find(params[:shop_profile_id])
#products = #shop_profile.shop_products
end
end
index.html.erb in shopprofiles
<%= link_to 'All Products', shop_profile_shop_products_path(#shop_profile) ,class: 'btn btn-primary' %>
on this line I get error that
ActionController::UrlGenerationError in ShopProfiles#index
Showing /home/mindfire/Desktop/project/training/Rails/grocery-shop/app/views/shop_profiles/index.html.erb where line #4 raised:
No route matches {:action=>"index", :controller=>"shop_products", :shop_profile_id=>nil} missing required keys: [:shop_profile_id]
the routes
shop_profile_shop_products GET /users/shop_profiles/:shop_profile_id/shop_products(.:format) shop_products#index
POST /users/shop_profiles/:shop_profile_id/shop_products(.:format) shop_products#create
new_shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/new(.:format) shop_products#new
edit_shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/:id/edit(.:format) shop_products#edit
shop_profile_shop_product GET /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#show
PATCH /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#update
PUT /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#update
DELETE /users/shop_profiles/:shop_profile_id/shop_products/:id(.:format) shop_products#destroy
And when I pass the shop_profile_id manually I get the desired page.
Thanks in advance for any help.
shop_profiles_controller.rb
class ShopProfilesController < ApplicationController
before_action :authenticate_user!, except: :show
after_action :verify_authorized, only: :shop_index
def new
#shop = ShopProfile.new
end
def index
#shops = current_user.shop_profiles
end
def show
#shop_profile = ShopProfile.find_by(id: params[:id])
#items = #shop_profile.shop_products.group(:category_id).where(category_id: params[:category_id])
end
def create
#shop = ShopProfile.new(shop_params)
#shop.build_address(address_params_shopkeeper)
if current_user.shop_profiles << #shop
flash[:success] = 'Shop Details added'
redirect_to root_path
else
flash[:error] = 'Shop Details not added'
render 'new'
end
end
def edit
#shop = current_user.shop_profiles.find_by(id: params[:id])
end
def update
#shop = current_user.shop_profiles.find_by(id: params[:id])
if #shop.update_attributes(shop_params) and #shop.address.update_attributes(address_params_shopkeeper)
flash[:success] = 'Updated Successfully'
redirect_to shop_profiles_path
else
flash[:danger] = 'Shop Details not Updated'
render 'edit'
end
end
end
But I think it has nothing to do with shop_profiles_controller.
I was calling the shop_product index page from there.
error log
Started GET "/users/shop_profiles" for 127.0.0.1 at 2016-03-31 16:36:34 +0530
Processing by ShopProfilesController#index as HTML
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1
Rendered shop_profiles/index.html.erb within layouts/application (2.3ms)
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.4ms)
ActionView::Template::Error (No route matches {:action=>"index", :controller=>"shop_products", :shop_profile_id=>nil} missing required keys: [:shop_profile_id]):
1: <div>
2: <%= link_to 'Add Shop' ,new_shop_profile_path, class: 'btn btn-primary' %>
3: <%= link_to 'Add New Product', new_product_path, class: 'btn btn-primary', method: :get %>
4: <%= link_to 'All Products', shop_profile_shop_products_path(#shop_profile) ,class: 'btn btn-primary' %>
5: </div>
6: <div>
7: <% if !#shops.nil? %>
app/views/shop_profiles/index.html.erb:4:in `_app_views_shop_profiles_index_html_erb___2474323268141556614_25251260'
Rendered /home/mindfire/.rvm/gems/ruby-2.2.0#localshop/gems/actionpack-4.2.5.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (9.0ms)............
Thanks #Зелёный
From your error log, it looks you are trying to send a nil in shop_profile_shop_products_path which is causing the issue.
Make sure #shop_profile is not nil.
You can do the followings if you want to avoid this issue:
<%= link_to 'All Products', shop_profile_shop_products_path(#shop_profile) ,class: 'btn btn-primary' if #shop_profile.present? %>
Hope it solves your problem!
Thanks you all for your responses .
The problem was a user can have multiple shop profiles . So I iterate through all shop profiles and call the index method and got the desired page.
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| %>
. . .
. . .
Rails can pass instance variables from controllers to views, you should use #post replace post in conmments/_form.html.erb.
<%= form_for([#post, #comment = #post.comments.build]) do |f| %>