質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2626閲覧

他人のプロフィールページに遷移させたい。

pay_561

総合スコア26

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/05/01 01:37

編集2021/05/01 02:04

##困っていること・前提
詳細ページからその投稿者のプロフィールページへ遷移させるリンクを作成したのですが、
投稿者のプロフィールに遷移されるのではなく現在ログインしているユーザのプロフィールに遷移されてしまいます。

URLを見るとprofiles/1やprofiles/2と正しいく遷移されているのですが現在ログインしているユーザの
プロフィールに遷移されてしまいます。

ユーザ情報にはdeviseを使っています。

##user.rb

class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one_attached :image has_many :posts, dependent: :destroy validates :name, presence: true end

##post.rb

class Post < ApplicationRecord has_many_attached :images has_one_attached :video belongs_to :user has_many :users end

##他人のプロフィールページへ遷移させるリンクのコード
posts/show.html.erb

<div class="profile-img"> <%= link_to profile_path(@post.user.id) do %> <% if @post.user.image.attached? %> <%= image_tag @post.user.image, class: 'h-profile' %> <% else %> <%= image_tag 'nouser.png', class: 'h-profile' %> <% end %> <% end %> </div>

##追記

##posts_controller.rb

class PostsController < ApplicationController before_action :authenticate_user! before_action :find_post, only: [:index, :show, :edit, :update, :destory] before_action :force_redirect_unless_my_post, only: [:edit, :update, :destory] def index @posts = Post.all.order(created_at: :desc) @posts = Post.page(params[:page]).per(25).order('updated_at DESC') end def show @post = Post.find_by(id: params[:id]) end def new @post = Post.new end def edit end def create @post = Post.new(post_params) @post.user = current_user if @post.save! redirect_to root_path, notice: '投稿成功!!' else render :new end end def update if @post.update(post_params) redirect_to root_path, notice: '投稿を更新しました。' else render :edit end end def destory if @post.destory redirect_to root_path, notice: '投稿を削除しました。' else redirect_to root_path, alert: '投稿が削除できませんでした。' end end private def post_params params.require(:post).permit( :thumbnail, :title, :version, :code, :description, :video, images: [], ) end def find_post @post = Post.find_by(params[:id]) end def force_redirect_unless_my_post return redirect_to root_path, alert: '自分の投稿ではありません' if @post.user != current_user end end

##profiles_controller.rb

class ProfilesController < ApplicationController before_action :authenticate_user! def show @profile = current_user end private end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

asm

2021/05/01 01:41

app/controller/susers_controller.rbの提示をお願いします
pay_561

2021/05/01 02:06

ご返信ありがとうございます。 app/controller/users_controller.rb というファイルはありませんでした。 一応posts_controller.rbとprofiles_controller.rbを追記しましたので ご教授いただけたら幸いです。
asm

2021/05/01 02:14

よく見たらprofiles_contorllerでしたね。すいません。
pay_561

2021/05/01 02:33

ご回答ありがとうございます。 current_userになっていたのが原因だったんですね!! ありがとうございます。助かりました!!!
guest

回答1

0

ベストアンサー

ruby

1class ProfilesController < ApplicationController 2 before_action :authenticate_user! 3 # 上の行はログインしてるユーザーのみにプロフィールページを見せるのならばこのまま 4 # ログインの必要がないのならば削除 5 6 def show 7 @profile = User.find(parms[:id]) 8 end 9end

かと、思います。

投稿2021/05/01 02:19

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問