マイページの投稿一覧をそのユーザーだけのものにしたいです。
users/show.html.haml
.wrapper %ul.post-user 【投稿者】 %li.post-user-name= @user.name .pagination= paginate @posts .main - @posts.each do |post| .contents .contents__item = link_to image_tag(post.image.url,:size => '300x200', class: 'post-image'), post_path(post.id) .content-title= post.name.truncate(24, omission: '...') .post-infomatoin - if user_signed_in? && current_user.id == post.user_id = link_to edit_post_path(post.id), class: "post-info", method: :get do = icon('fas', 'edit', class: "post-edit") = link_to post_path(post.id), class: "post-info-destroy", method: :delete do = icon('fas', 'trash-alt', class: "post-destroy") .pagination= paginate @posts
users.controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) @posts = @user.posts.order('created_at DESC') @posts = Post.page(params[:page]).per(10).order('updated_at DESC') end end
posts.controller.rb
class PostsController < ApplicationController def index @posts = Post.includes(:user).order("created_at DESC") @posts = Post.page(params[:page]).per(10).order('updated_at DESC') end def new @post = Post.new end def create @post = Post.create(post_params) if @post.save redirect_to :root else render :new, notice: "画像を選択してください" end end def show @post = Post.find(params[:id]) @comment = Comment.new @comments = @post.comments.includes(:user).order('created_at DESC') end def destroy post = Post.find(params[:id]) if post.user_id == current_user.id post.destroy end redirect_to :root end def edit @post = Post.find_by(id: params[:id]) end def update @post = Post.find_by(id: params[:id]) if @post.update(post_params) redirect_to :root else render :edit end end private def post_params params.require(:post).permit(:text, :image, :name).merge(user_id: current_user.id) end end
models/user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :name, presence: true, uniqueness: true has_many :posts has_many :comments, dependent: :destroy has_many :favorites, dependent: :destroy def already_favorited?(post) self.favorites.exists?(post_id: post.id) end end
models/post.rb
class Post < ApplicationRecord belongs_to :user has_many :comments, dependent: :destroy has_many :favorites, dependent: :destroy mount_uploader :image, ImageUploader validates :image, presence: true validates :name, length: { maximum: 30 } end
routes.rb
Rails.application.routes.draw
1 devise_for :users 2 get '/users', to: redirect("/users/sign_up") 3 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 4 root to: 'posts#index' 5 resources :posts do 6 resources :comments, only: :create 7 resources :favorites, only: [:create, :destroy] 8 end 9 resources :users, only: [:show] 10 11end
お願いします!
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/17 01:53