The following loop shows each comment associated to the specific item and lets the comment owner(user) to edit or delete a comment. This works just fine.
Now I would the admin to be able to delete a comment. The only problem is that when I click on the delete... all of the comments associated to the specific item get deleted and not just that one.
Any idea how to fix this?
<% #comments.each do |comment| %>
<% if current_user == comment.user %>
<%= link_to "Delete", destroy_item_comment_path(comment.item_id, comment.id),
method: :delete, data: { confirm: I18n.t("comment_delete_alert")}, :class=>"glyphicon glyphicon-trash" %>
<% elsif current_admin %>
<%= link_to "Delete", admin_destroy_item_comment_path(comment.item_id, comment.id),
method: :delete, data: { confirm: I18n.t("comment_delete_alert")}, :class=>"glyphicon glyphicon-trash" %>
<% end %>
<% end %>
controller method:
before_action :find_comment, only: [:destroy]
def destroy
#comment.destroy
redirect_back(fallback_location: root_path)
flash[:notice] = "Comment has been deleted"
end
def find_comment
#comment = #item.comments.find(params[:id])
end
Update 1
server logs when deleting a comment as an admin
Comment Load (0.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = $1 [["user_id", 213]]
(0.2ms) BEGIN
SQL (0.4ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 14]]
SQL (0.4ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 15]]
SQL (0.3ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 16]]
(1.2ms) COMMIT
Completed 302 Found in 14123ms (ActiveRecord: 36.3ms)
My hunch is that your routes are not configured properly. You can hit
rails routes
to check admin_delete_comment_path configuration.
Try changing
admin_destroy_item_comment_path(comment.item_id,comment.id)
to:
destroy_item_comment_path(commend.item_id, comment.id)
Related
After pressing the follow/unfollow toggle button, I receive an ActionView::MissingTemplate exception for the _follow.html.erb. I cannot seem to get past this point with the follow/unfollow button. I tried 6 different approaches and none seem to render the correct results for me. How can setup the follow/unfollow toggle button to work correctly in my index/show view??
Server Development Log
Started GET "/u/jimmydean/follow" for 127.0.0.1 at 2018-02-25 00:43:41 -0500
Processing by UsersController#follow as JS
Parameters: {"id"=>"jimmydean"}
User Load (3.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."slug" = $1 LIMIT $2 [["slug", "jimmydean"], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (1.0ms) INSERT INTO "notifications" ("user_id", "actor_id", "notify_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["actor_id", 2], ["notify_type", "follow"], ["created_at", "2018-02-25 05:43:42.022447"], ["updated_at", "2018-02-25 05:43:42.022447"]]
(0.0ms) COMMIT
Completed 500 Internal Server Error in 1039ms (ActiveRecord: 5.0ms)
ActionView::MissingTemplate (Missing template users/follow, application/follow with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim, :coffee, :jbuilder]}. Searched in:
index.html.erb
<div class="col-md-4">
<div class="others-like-me-box">
<div class="others-like-me-box-header user">
<%= render partial: 'users/shared/follow', locals: {users: #users} %>
</div>
</div>
</div>
_follow.html.erb
<ul class="list-unstyled">
<% #users.each do |user| %>
<li>
<div class="media">
<%= user_avatar_dashboard(user) %>
<div class="media-body">
<h6 class="mt-0 mb-1"><%= fa_icon 'user-o' %><strong> <%= link_to user.username, user %></strong></h6>
<div class="cool-ppl-to-follow-btn-pos">
<% if current_user.id != user.id %>
<% if user.followed_by?(current_user) %>
<%= link_to 'Unfollow', unfollow_user_path(user), class: 'btn btn-outline-danger', remote: true %>
<% else %>
<%= link_to 'Follow', follow_user_path(user), class: 'btn btn-outline-success', remote: true %>
<% end %>
<% end %>
</div>
</div>
</div>
</li>
<% end %>
</ul>
users_controller.rb
def follow
#user = User.friendly.find(params[:id])
if current_user
current_user.follow(#user)
end
respond_to do |format|
format.js
end
end
def unfollow
#user = User.friendly.find(params[:id])
if current_user
current_user.stop_following(#user)
end
respond_to do |format|
format.js
end
end
def block
#user = User.friendly.find(params[:id])
if current_user
current_user.block(#user)
end
respond_to do |format|
format.js
end
end
def unblock
#user = User.friendly.find(params[:id])
if current_user
current_user.unblock(#user)
end
respond_to do |format|
format.js
end
end
routes.rb
resources :users, only: [:show, :index, :update], path: 'u' do
get 'users/:username' => 'users#show'
patch 'users/:username', to: 'users#update'
member do
get :follow
get :unfollow
get :block
get :unblock
end
end
follow.js.erb
<% if #user.followed_by?(current_user) %>
$("#user-<%= #user.id %>").html("<%= link_to 'Unfollow', unfollow_user_path(#user), remote: true %>")
<% else %>
$("#user-<%= #user.id %>").html("<%= link_to 'Follow', follow_user_path(#user), remote: true %>")
<% end %>
UPDATE
When I click the follow button, the state doesn't change, nor does the user actually follow the target user. My server log shows this below.
Started GET "/u/jimmydean/follow" for 127.0.0.1 at 2018-02-25 12:01:30 -0500
Processing by UsersController#follow as JS
Parameters: {"id"=>"jimmydean"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."slug" = $1 LIMIT $2 [["slug", "jimmydean"], ["LIMIT", 1]]
(0.0ms) BEGIN
SQL (75.1ms) INSERT INTO "notifications" ("user_id", "actor_id", "notify_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["user_id", 1], ["actor_id", 2], ["notify_type", "follow"], ["created_at", "2018-02-25 17:01:30.780542"], ["updated_at", "2018-02-25 17:01:30.780542"]]
(151.1ms) COMMIT
Rendering users/follow.js.erb
DEPRECATION WARNING: Setting custom parent classes is deprecated and will be removed in future versions. (called from parent_class_name at C:/Ruby24-x64/lib/ruby/gems/2.4.0/bundler/gems/acts_as_follower-c5ac7b9601c4/lib/acts_as_follower/follower_lib.rb:10)
Follow Load (0.0ms) SELECT "follows".* FROM "follows" WHERE "follows"."followable_id" = $1 AND "follows"."followable_type" = $2 AND "follows"."blocked" = $3 AND "follows"."follower_id" = $4 AND "follows"."follower_type" = $5 ORDER BY "follows"."id" ASC LIMIT $6 [["followable_id", 1], ["followable_type", "User"], ["blocked", "f"], ["follower_id", 2], ["follower_type", "User"], ["LIMIT", 1]]
Rendered users/follow.js.erb (4.0ms)
Completed 200 OK in 1399ms (Views: 1102.6ms | ActiveRecord: 228.2ms)
For UsersController#follow to automatically find the unobtrusive JS partial, it should also be placed within the users folder (i.e. app/views/users/follow.js.erb).
Your code looks fine, but since your partial is in users/shared/_follow.html.erb, you might have accidentally placed the JS partial in users/shared as well.
I am getting the below error and unsure how to correct it - your help would be much appreciated.
when at a users show page, i do not get the error, but when at for example at any other page...such as the events index.html page the error displays
No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "ian#gmail.com"
expanded error in terminal
Started GET "/events" for ::1 at 2016-12-17 12:44:43 +0000
Processing by EventsController#index as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 4]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."email" IS NULL LIMIT 1
Subscription Load (0.1ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."title" = ? LIMIT 1 [["title", "premium"]]
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ? [["user_id", 4], ["status", "requested"]]
(0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friend_id" WHERE "friendships"."user_id" = ? AND "friendships"."status" = ? [["user_id", 4], ["status", "requested"]]
Rendered shared/_content_dropdownbox_friendrequest.html.erb (4.8ms)
Rendered shared/_header.html.erb (10.7ms)
Rendered events/index.html.erb within layouts/application (11.8ms)
Completed 500 Internal Server Error in 23ms
ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"friendships", :friend_id=>#<User id: 5, email: "ian#gmail.com", encrypted_password: "$2a$10$PMO5FPBzjjnFHI/ye8rfP.ONtHP3gagXomj1sbruBXH..."
route file
Rails.application.routes.draw do
resources :friendships, only: [:create, :update, :destroy]
devise_for :users
resources :users
end
views/shared/_content_dropdownbox_friendrequest.html.erb
<ul>
<% current_user.requested_friends.each do |requester| %>
<li>
<%= link_to(image_tag(requester.image_url, :alt => "image", :class =>"#", id: ""), user_path(requester)) %>
<%= link_to truncate("#{requester.firstname} #{requester.lastname}", length: 23), user_path(requester) %>
<%= link_to 'Accept', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "update", method: :put %>
<%= link_to 'Decline', friendship_path(user_id: current_user, friend_id: requester), controller: "friendships", action: "decline", method: :delete %>
</li>
<div class="clear"></div>
<% end %>
</ul>
frienships_controller
class FriendshipsController < ApplicationController
before_action :authenticate_user!
before_filter :setup_friends
# Sends a friend request.
# We'd rather call this "request", but that's not allowed by Rails.
def create
Friendship.request(#user, #friend)
flash[:notice] = "Request sent."
redirect_to :back
end
# Accepts a friend request.
# We'd rather call this "accept", but that's not allowed by Rails.
def update
#user = User.friendly.find(params[:user_id])
#friend = User.friendly.find(params[:friend_id])
if #user.requested_friends.include?(#friend)
Friendship.accept(#user, #friend)
flash[:notice] = "Connection with #{#friend.firstname} accepted!"
else
flash[:notice] = "No connect request from #{#friend.firstname}."
end
redirect_to :back
end
def destroy
#user = User.friendly.find(params[:user_id])
#friend = User.friendly.find(params[:friend_id])
if #user.requested_friends.include?(#friend) #decline
Friendship.breakup(#user, #friend)
redirect_to :back
elsif #user.pending_friends.include?(#friend) #cancel
Friendship.breakup(#user, #friend)
redirect_to :back
elsif #user.friends.include?(#friend) #delete
Friendship.breakup(#user, #friend)
redirect_to :back
end
end
private
def setup_friends
#user = User.find(current_user.id)
#friend = User.find_by_email(params[:id])
end
end
i am unsure how to solve the error, i tried something like the below but does not work:
<span>
<%= form_for friendship, url: friendship_path(requester), method: :put do |f| %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :friend_id, value: requester.id %>
<%= submit_tag "Accept_test", controller: "friendships", action: "update", class: "btn btn_add_friend" %>
<% end %>
</span>
update controller is waiting for an id parameter, while the only parameters you pass are user_id and friend_id.
You can change your routes to the following
resources :friendships, only: [:create, :destroy] do
collection do
put :update
end
end
I am stumped as to why this isn't working. I have a review system, and a link to create a new review passing 2 attributes; user_id and gigid. Both id's are being generated as integers but when I save the review, the gigid saves as 'nil' rather than the integer.
View:
<%= link_to 'Click here to rate this user', new_user_review_path(:user_id => request.user.id, :gigid => request.gig.id) %>
Here both ID's are set correctly (according to server output). User_id is the user which is to be reviewed (works correctly) and gigid is a reference for post validation. (works correctly until I try and save)
_form :
<%= simple_form_for([#user, #user.reviews.build]) do |f| %>
<div id="rating-form">
<label>Rating</label>
</div>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
Controller:
def new
if user_signed_in?
#review = current_user.reviews.new
else
redirect_to(root_url)
flash[:danger] = "You must log in to rate a user"
end
end
def create
#review = #user.reviews.new review_params
#review.reviewed_id = current_user.id
if #review.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def review_params
params.require(:review).permit(:rating, :comment, :gigid, :user_id)
end
end
Server output when clicking link:
Started GET "/users/21/reviews/new?gigid=17&locale=en" for 127.0.0.1 at 2016-01-13 18:08:26 +0100
Processing by ReviewsController#new as HTML
Parameters: {"gigid"=>"17", "locale"=>"en", "user_id"=>"21"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered reviews/_form.html.erb (4.9ms)
I then add a comment and a rating and click create.
Server output when clicking create:
Started POST "/users/21/reviews?locale=en" for 127.0.0.1 at 2016-01-13 18:10:44 +0100
Processing by ReviewsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"RToZwvodvXxQp1/spEhgemO/oi+4rof8jREuLw27CcEsEBqFWGLeYne1CgH3kvWu9OzAV+bHvOk9g9nq8JMPnw==", "review"=>{"rating"=>"5", "comment"=>"sdfsdfdd"}, "commit"=>"Create Review", "locale"=>"en", "user_id"=>"21"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 21]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "reviews" ("rating", "comment", "user_id", "reviewed_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["rating", 5], ["comment", "sdfsdfdd"], ["user_id", 21], ["reviewed_id", 1], ["created_at", "2016-01-13 17:10:44.675386"], ["updated_at", "2016-01-13 17:10:44.675386"]]
(93.6ms) commit transaction
I notice that 'gigid' is being completely ignored here, but EVERYTHING else saves fine.
I don't understand why one attribute saves and the other doesn't, any insight would be greatly appreciated!
Your form doesn't have anything for gigid to send it to create action. You can use hidden field in the form like below
<%= simple_form_for([#user, #user.reviews.build]) do |f| %>
<div id="rating-form">
<label>Rating</label>
</div>
<%= f.input :comment %>
<%= f.input :gigid, :as => :hidden, :input_html => { :value => params[:gigid] }
<%= f.button :submit %>
<% end %>
There is no gigid in review params. Look carefully on your params when you are performing POST: "review"=>{"rating"=>"5", "comment"=>"sdfsdfdd"}. Just review and comment. And after it you are doing:
params.require(:review).permit(:rating, :comment, :gigid, :user_id)
I am using ruby on rails try to update user information, but when I submit, the console will show an error saying the user exists and redirect to the correct page. What's wrong with my code?
The error message:
Started PATCH "/users/6" for ::1 at 2015-06-08 21:27:00 -0500
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sJm38g36DAYDo4eXdpcIPRX0e40Jp6cECmMwEvhCAEhTlDwwmmgOfXZqeczglNmJ4K9pQXiyXAsRsgP/C8lScg==", "name"=>"test123", "department"=>"123", "commit"=>"Update User", "id"=>"6"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]] CACHE (0.0ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "6"]] (0.1ms)
begin transaction
User Exists (0.2ms)
SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'test#test.com' AND "users"."id" != 6) LIMIT 1 (0.1ms)
rollback transaction
Redirected to http://localhost:3000/users/6
Completed 302 Found in 9ms (ActiveRecord: 0.5ms)
Started GET "/users/6" for ::1 at 2015-06-08 21:27:00 -0500
Processing by UsersController#show as HTML
Parameters: {"id"=>"6"} User Load (0.1ms)
SELECT "users".* FROM "users" WHERE "users"."id"
= ? LIMIT 1 [["id", 6]]
Rendered users/show.html.erb within layouts/application (0.1ms)
User Load (0.2ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]] CACHE (0.0ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Completed 200 OK in 66ms (Views: 64.3ms | ActiveRecord: 0.3ms)
The edit page
<h1 class="center">Edit name</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_tag "/users/#{#user.id}", :method => 'patch' do %>
<p>
<%= label_tag :name %>
<%= text_field_tag :name, #user.name %>
</p>
<p>
<%= label_tag :department %>
<%= text_field_tag :department, #user.dept %>
</p>
<input type="submit" name="commit" value="Update User">
<% end %>
</div>
</div>
The controller is like this
class UsersController < ApplicationController
before_action :authorize, only: [:show, :edit, :update]
def authorize
#user = User.find_by(id: params[:id])
if #user.blank? || session[:user_id] != #user.id
redirect_to root_url, notice: "Nice try!"
end
end
def new
#user = User.new
end
def show
end
def edit
end
def update
#user = User.find_by(id: params[:id])
#user.name = params[:name]
#user.dept = params[:department]
#user.save
redirect_to user_path(#user.id)
end
def create
#user = User.new(email: params[:email],
name: params[:name],
password: params[:password],
role: params[:role],
dept: params[:dept])
if #user.save
redirect_to root_url, notice: "Thanks for signing up."
else
render "new"
end
end
end
The router concerning this part is like:
# sign up
get '/signup' => 'users#new'
post '/users' => 'users#create'
get '/users/:id' => 'users#show', as: :user
get '/users/:id/edit' => 'users#edit', as: 'edit_user'
patch '/users/:id' => 'users#update'
The problem is in the form_tag,it should be like this
<%= form_tag({:action => :update}, {:method => :patch}) do %>
Also your code for form_tag looks vulnerable. Changing it to like this will be better.
<%= form_tag update_user_path(#user) do %>
or
<%= form_tag user_path(#user), :method => :patch do %>
Are you using rails 4?
You should update your controller to conform to strong_parameters if you are.
def update
#user = User.find(params[:id)
if #user.update_attributes(user_params)
redirect_to user_path(#user.id)
else
render :edit
end
end
private
def user_params
params.require(:user).permit(:name, :dept)
end
Doing this will mean that you have to wrap your name and dept params inside a user scope, eg.
user: { name: "Howard Moon", dept: "Zookeeper" }
But is the standard way to handle params in the controller.
Hope this helps!
EDIT: Link to Strong Parameters which does a better job at explaining this than I can. Haha
I create user->posts->comments associations, but comments don't work right.When I commented post, comment save to data base, column user_id was recorded (ok), but column post_id is empty.
There is partial _post_list.html.erb
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<%= render 'shared/comment' %>
<%= post.user.name %>
</tr></br></br>
<% end %>
There is partial _comment.html.erb
<%= form_for(#comment) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
controller comments_controller
class CommentsController < ApplicationController
def new
#comment = Comment.new
end
def create
#comment = current_user.comments.build(comment_params)
if #comment.save
redirect_to root_url
else
render 'static_pages/home'
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
UPD there is server log
Started POST "/posts/6/comments" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZvyHKxSdH0a1rf79yaWmo9N/pD9/YhSQ9Ek8bdMLhOI=", "comment"=>{"body"=>"stackoverflow"}, "commit"=>"commented", "post_id"=>"6"}
User Load (4.9ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
(0.2ms) begin transaction
SQL (0.6ms) INSERT INTO "comments" ("body", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["body", "stackoverflow"], ["created_at", "2014-11-16 20:28:26.544500"], ["updated_at", "2014-11-16 20:28:26.544500"], ["user_id", 5]]
(131.7ms) commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 152ms (ActiveRecord: 137.4ms)
Started GET "/" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by StaticPagesController#home as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
Rendered shared/_post_form.html.erb (2.4ms)
Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
Rendered shared/_comment.html.erb (1.8ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 5]]
Rendered shared/_comment.html.erb (2.8ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (4.0ms)
CACHE (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (1.5ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_comment.html.erb (2.9ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered shared/_post_list.html.erb (45.3ms)
Rendered static_pages/home.html.erb within layouts/application (52.1ms)
Rendered layouts/_header.html.erb (1.2ms)
Completed 200 OK in 364ms (Views: 347.0ms | ActiveRecord: 4.5ms)
controller static_pages_controller
class StaticPagesController < ApplicationController
def index
end
def show
end
def home
#post = current_user.posts.build
if signed_in?
end
#posts = Post.all
#comment = current_user.comments.build
end
end
view home.html.erb
<%= render 'shared/post_form' %>
<%= render 'shared/post_list' %>
How fix?
sorry for my bad English
You are far from showing enough code to allow us to understand what can happen. But I feel strange that your form does not include somewhere a reference to post.id or whatever you name the identifier for post objects.
The controller will only be able to use :
input fields of the form (here text of comment)
session stored values (user id)
If the current post id is neither in form fields, not in session, the controller will not get id, it you cannot store it in database.
It looks like you are looping through all of your posts and creating a comment form on the same page for each. You may want to set your comment routes up as nested resources by wrapping it within the post resource.
# config/routes.rb
resources :posts do
resources :comments
end
Pass your #post object into the partial like so:
<%= render 'shared/comment', post: post %>
Then you should be able to create your comment form like so:
<%= form_for([post,#comment]) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>
By including that post, you are telling the comment which post it belongs to. Don't forget to update your strong parameters in the controller if you are using them.
EDIT: Based on your update, you'll need to add :post_id to the permit at the bottom of our controller.