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

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

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

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

Ruby on Rails 6

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

Ruby on Rails

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

Q&A

1回答

3193閲覧

ユーザー情報を表示させたい

toratail

総合スコア12

Ruby

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

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

1クリップ

投稿2020/10/13 08:49

編集2022/01/12 10:55

Twitterのクローンみたいなものを作成中なのですが、ユーザー情報が表示されません。

app > views > users >show.html.erb <div class="main"> <div class="inner"> <div class="user__wrapper"> <h2 class="page-heading"> <%= "#{@user.name}さんの情報"%> </h2> <table class="table"> <tbody> <tr> <th class="table__col1">名前</th> <td class="table__col2"><%= @user.name %></td> </tr> <tr> <th class="table__col1">プロフィール</th> <td class="table__col2"><%= @user.profile %></td> </tr> <tr> <th class="table__col1">所属</th> <td class="table__col2"><%= @user.occupation %></td> </tr> <tr> <th class="table__col1">役職</th> <td class="table__col2"><%= @user.position %></td> </tr> </tbody> </table> <h2 class="page-heading"> <%= "#{@user.name}さんのプロトタイプ"%> </h2> <div class="user__card"> <%# 部分テンプレートでそのユーザーが投稿したプロトタイプ投稿一覧を表示する %> <%= render partial: 'prototypes/prototype', collection: @prototypes %> </div> </div> </div> </div> ``` ``` app > model > 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 #nameカラムが空の状態ではDBに保存できないということ validates :name, presence: true validates :profile, presence: true validates :occupation, presence: true validates :position, presence: true has_many :prototypes has_many :comments end ```

app > controller > user_controller.rb

class UsersController < ApplicationController
end

def show
@user = User.find(params[:id])
@prototypes = current_user.prototypes
end

いまいち仕組みが掴めていません。 ご教授宜しくお願いします @user.name〜に変更後 また別のエラーが出てきました。 NameError in Prototypes#index Showing /Users/taigasoma/projects/protospace-30528/app/views/prototypes/_prototype.html.erb where line #2 raised: uninitialized constant Prototype::Image

app > views > prototypes > _prototype.html.erb

<div class="card"> #下記がマークされたエラー文です。 <%= link_to image_tag(prototype.image, class: :card__img ) %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.id), method: :get, class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %> </div> </div> ```
app > views > prototype > _prototype.html.erb <div class="card"> <%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.id), method: :get, class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %> </div> </div> 二行目の <%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %> にてNameErrorが発生しました。 index.htmlは <main class="main"> <div class="inner"> <%# ログインしているときは以下を表示する %> <% if user_signed_in? %> <div class="greeting"> こんにちは、 <%= link_to "#{current_user.name}さん", "/users/#{current_user.id}", class: :greeting__link %> </div> <% end %> <%# // ログインしているときは上記を表示する %> <div class="card__wrapper"> <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> <div class="prototypes"> <%= render partial: 'prototypes/prototype', collection: @prototypes %> </div> </div> </div> </main> コントローラーは、 class PrototypesController < ApplicationController before_action :authenticate_user!, except: [:index, :show] def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path else render :new end end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new @comments = @prototype.comments.includes(:user) end def edit @prototype =Prototype.find(params[:id]) end def update prototype = Prototype.find(params[:id]) if prototype.update(prototype_params) redirect_to prototype_path else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end routes.rbは Rails.application.routes.draw do devise_for :users root to: 'prototypes#index' resources :prototypes do resources :comments, only: :create end resources :users, only: :show end modelsは class Prototype < ApplicationRecord has_one_attached :image has_many :comments has_many :commnets, dependent: :destroy has_many :image, dependent: :destroy validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true belongs_to :user end

こちらが詳細のコードになります。
宜しくお願いいたします。

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

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

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

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

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

hatsu

2020/10/13 12:57

app > controller > user_controller.rbは、以下の書き間違いでしょうか? class UsersController < ApplicationController def show @user = User.find(params[:id]) @prototypes = current_user.prototypes end end @user = User.find(params[:id])で@userが取得できていないのかと思っているのですが、ユーザーを見るさいのURLは/users/1とかでしょうか?
toratail

2020/10/13 14:02

URLは/users/1になります!
toratail

2020/10/13 14:10

class UsersController < ApplicationController def show @user = User.find(params[:id]) @prototypes = current_user.prototypes end end こちらに修正しましたら、app > views >showのページでそれぞれ <%= "#{@prototypes.user.name}さんの情報"%> <%= @prototypes.user.name %> <%= @prototypes.user.profile %> <%= @prototypes.user.occupation %> <%= @prototypes.user.position %> <%= "#{@prototypes.user.name}さんのプロトタイプ"%> と記述すればよろしいでしょうか?
hatsu

2020/10/13 14:29

いえ、@userのままでとユーザー情報が表示されないでしょうか。 表示されなければデータがないとかかなと思いました
toratail

2020/10/13 14:36

データはございます。 showの書き方は@user.nameでよろしかったでしょうか?
hatsu

2020/10/13 14:39

はい、show.html.erbはそのままで良さそうに思ってます
toratail

2020/10/13 14:42

ありがとうございます。データベースには残っているのは確認できるのですが・・・
hatsu

2020/10/13 14:46

エラーも表示されないです? UserID1にはどんなデータが入っているのが確認できますでしょうか? また今気づきましたが、ファイル名はusers_controller.rbになっていますでしょうか?user_controller.rbとなっていませんでしょうか。
toratail

2020/10/13 14:51

usersでした申し訳ありません。 データベースusers id1には、 email encryted_password name profile occupation position になります。 データは全てaが記述してあります。
hatsu

2020/10/13 15:00

users_controller.rbにするとどうなりますでしょうか? エラーが表示されるのか、ユーザー情報だけ表示されずに他のHTMLの文字列(たとえば「役職」)とかだけ表示されているのでしょうか?質問に追記でスクショとか貼っていただくとわかりやすいかもです。
toratail

2020/10/13 15:16

NoMethodEtrorになります! @userだけだと、空のまま表示されます。 今は違うエラーが出ています!
hatsu

2020/10/13 15:27

追記いただいた情報を見ると、ひとまず@userの情報の取得はできている気がします(ひとまず確認するには<%= render partial: 'prototypes/prototype', collection: @prototypes %>をコメントアウトすればいいと思います) また追記いただいたエラーはprototypeにimageがないような感じがします。 <%= link_to image_tag(prototype.image, class: "card__img" ), if prototype.image %> とかにして解決しなかったら、また別の質問を立てていただいて、prototypeモデルの情報などを載せていただけると嬉しいかもです!
toratail

2020/10/14 01:35

ありがとうございます。別の質問フラグ作ったのでおねがいいたします。 お伝えいただいたやつで記入しましたが、 ActionView::SyntaxErrorInTemplate in PrototypesController#index が出てきました。
hatsu

2020/10/14 01:39

まずは<%= render partial: 'prototypes/prototype', collection: @prototypes %>をコメントアウトすると動きますでしょうか? これが動くなら、この質問の回答として解決済みにしたいと考えています。 ActionView::SyntaxErrorInTemplate in PrototypesController#index は他の変更によるエラーだと思われます。 proptypes/index.html.erbがないと言われているエラーです。
toratail

2020/10/14 01:41

コメントアウトしてもActionView::SyntaxErrorInTemplate in PrototypesController#indexが表示されてしまいます!
hatsu

2020/10/14 01:42

proptypes/index.html.erbは作りましたでしょうか?
toratail

2020/10/14 01:47

作ってあります!
hatsu

2020/10/14 01:51

/users/1にアクセスしてもそのエラーが出るのでしょうか? proptypes_controller.rbにdef index;endがありviews/proptypes/index.html.erbがあればそんなこと起きなそうですふしぎです。railsを一度閉じてもう一度立ち上げるなどもしてみてください。 それでもエラーであれば、routes.rb、proptypes_controller.rb、proptypes.html.erb、エラーが出るURLとそのエラー文あたりを追記いただきたいです。(エラーが多く存在していたため、元の質問の回答からはなれてしまっていますが、、)
toratail

2020/10/14 01:53

localhost:3000で最初にエラーになってしまいます。
hatsu

2020/10/14 01:56

また別のエラーが出ているのですね。どんなエラーでしょうか?(エラーが出たらその内容も教えていただけると!)
toratail

2020/10/14 02:03

NameError in Prototypes#index Showing /Users/taigasoma/projects/protospace-30528/app/views/prototypes/_prototype.html.erb where line #2 raised: uninitialized constant Prototype::Image こちらになります
hatsu

2020/10/14 02:05

あ、そのエラーですね。そちらも一時的にコメントアウトしたらどうでしょうか
toratail

2020/10/14 02:07

コメントアウトしユーザー情報の記載の問題は解決できていました! 本当にありがとうございます。。。
guest

回答1

0

controllerファイルの修正が必要です。
ファイルの外にdef showが定義されていたので、以下のような修正が回答になりそうです。

class UsersController < ApplicationController def show @user = User.find(params[:id]) @prototypes = current_user.prototypes end end

追記いただいた NameError in Prototypes#indexについては、べつの質問となったためここでは非対応です。

投稿2020/10/14 02:21

hatsu

総合スコア1809

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問