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

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

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

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

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

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

Q&A

解決済

1回答

650閲覧

device使用時に、投稿者一覧を作成して、その投稿者のプロフィールと投稿を表示させたい。

takahashi-city

総合スコア2

Ruby

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

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/06/22 13:41

編集2021/06/22 14:08

前提・実現したいこと

投稿者一覧を作成して、投稿者名をクリックすると
投稿者ごとプロフィール画面に移りプロフィールと
そのユーザーごと投稿を表示させたいです。
現在、deviceの導入は完了していて、ログインユーザー
のプロフィールを表示させるところまではできました。
current_userメソッドでログインしているユーザーを
取得することができることは理解しておりますが、
そのほかのユーザーの情報を取得する方法がわかりません。
その為、投稿者一覧を作成して、
投稿者のプロフィール画面に移るための方法や
そのプロフィール画面にその投稿者の投稿を表示させる方法がわかりません。

下記の「routes」のようにresourcesのindexとshowだけを
設定すれば、URLにidが表示されて投稿者ごとのプロフィールや投稿を
表示できるかと思ったのですが、そもそもログインの際に下記のような
エラーが出てきてしまいます。おそらくログインの際に設定した
showアクションの@user=User.find(params[:id])で取ってきてしまっているようですが、
なぜそうなるのかがわかりません。。。。

発生している問題・エラーメッセージ

エラーメッセージ Couldn't find User with 'id'=sign_in

該当のソースコード

routes

1Rails.application.routes.draw do 2 root 'static_pages#home' 3 get "static/show" => "static_pages#show" 4 resources :users, :only => [ :index, :show ] 5 devise_for :users, controllers: { :omniauth_callbacks => "omniauth_callbacks" , registrations: 'users/registrations' }

userscontroller

1class UsersController < ApplicationController 2 3 def new 4 end 5 6 def index 7 @users = User.all 8 end 9 10 def update 11 if current_user.update(user_params) 12 redirect_to user_path(current_user) 13 else 14 render :edit 15 end 16 end 17 18 def show 19 @user=User.find(params[:id]) 20 end 21 22end 23
Prefix Verb URI Pattern Controller#Action root GET / static_pages#home static_show GET /static/show(.:format) static_pages#show users GET /users(.:format) users#index user GET /users/:id(.:format) users#show new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format) omniauth_callbacks#passthru user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format) omniauth_callbacks#twitter new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new user_confirmation GET /users/confirmation(.:format) devise/confirmations#show POST /users/confirmation(.:format) devise/confirmations#create new_user_unlock GET /users/unlock/new(.:format) devise/unlocks#new user_unlock GET /users/unlock(.:format) devise/unlocks#show POST /users/unlock(.:format) devise/unlocks#create rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create ---

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

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

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

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

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

guest

回答1

0

ベストアンサー

resources :users, :only => [ :index, :show ]
devise_for :users, controllers: { :omniauth_callbacks => "omniauth_callbacks" , registrations: 'users/registrations' }

rountes.rbは記述する順番が重要です。
devise_forresourcesより先に記述してください

投稿2021/06/24 23:26

asm

総合スコア15147

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

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

takahashi-city

2021/06/29 11:35

解決しました!!ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問