resources :users do
resources :posts, only: %i[create destroy]
end
class PostsController < ApplicationController
before_action :set_user
before_action :set_post, expect: [:create]
def create
@post = @user.posts.build(params_post)
if @post.save
flash[:success] = "Post created!"
redirect_to user_path(@user)
else
@posts = Post.all
render "users/show"
end
end
def destroy
@post.destroy
flash[:success] = "Post deleted!"
redirect_to user_path(@user)
end
private
def set_user
@user = User.find(params[:user_id])
end
def set_post
@post = @user.posts.find(params[:id])
end
def params_post
params.require(:post).permit(:content)
end
end
# views/users/show.html.erb
<article>
<% @user.posts.each do |post| %>
<p>
<%= post.content %>
<small><%= formatted_at_time(post.created_at) %></small>
</p>
<% if post.user == current_user %>
<%= link_to "Delete", user_post_path(@user, post),
data: { method: :delete, confirm: "Are you sure?" }, class: "btn btn-danger" %>
<% end %>
<% end %>
</article>
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"posts", :id=>nil, :user_id=>"4"}, missing required keys: [:id]): 19:
20: 21: <% if post.user == current_user %> 22: <%= link_to "Delete", user_post_path(@user, post), 23: data: { method: :delete, confirm: "Are you sure?" }, class: "btn btn-danger" %> 24: <% end %> 25: <% end %>
Thank you in advance!
post
is nil. Are there any@user.posts
records? Alternatively, what doesrails routes
tell you? There'll be lots there, but look for theuser_post*
paths. – JohnP