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

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

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

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

Ruby

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

Q&A

解決済

1回答

501閲覧

form_withのtext_fieldに入力した値が表示しない

huu

総合スコア5

Ruby on Rails 5

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

Ruby

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

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

0グッド

0クリップ

投稿2020/06/05 04:57

前提・実現したいこと

Ruby on Railsの勉強でオリジナルのWebアプリを作成しています。
user詳細ページでBootstrap4のカードの中にprofileを表示させたいです。

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

profile登録フォームに入力した値がエラーがでないのにカードに表示されない。

該当のソースコード

models/user.rb

class User < ApplicationRecord has_one :profile, dependent: :destroy end

models/profile.rb

class Profile < ApplicationRecord belongs_to :user end

users/controller.rb

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

profiles_controller.rb

def new @profile = current_user.build_profile end def create @profile = current_user.build_profile(profile_params) @profile.user_id = current_user.id if @profile.save flash[:primary] = 'プロフィールを登録しました' redirect_to current_user else flash[:danger] = 'プロフィールの登録に失敗しました' render :new end end private def profile_params params.require(:profile).permit(:artist, :song) end

users/show.html.erb

<div class="card"> <div class="card-header"> <h3><i class="fas fa-user-circle mr-2"></i><%= @user.name %></h3> </div> <div class="card-body"> <ul> <p>好きなアーティスト</p> <li><%= @profile.artist %></li> <p>好きな曲</p> <li><%= @profile.song %></li> </ul> </div> </div>

profiles/new.html.erb

<div class="row"> <div class="col-sm-6 offset-sm-3"> <%= form_with(model: @profile, local: true) do |f| %> <div class="form-group"> <%= f.label :artist, '好きなアーティスト' %> <%= f.text_field :artist, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :song, '好きな曲' %> <%= f.text_field :song, class: 'form-control' %> </div> <%= f.submit '登録', class: 'btn btn-primary btn-block' %> <% end %> </div> </div>

試したこと

下記のように変更してみたんですが、これだとそれまで表示していた好きなアーティストと好きな曲の文字まで消えてしまい、profile登録フォームに入力した値も表示されませんでした。

users/show.html.erb

<% @profiles.each do |profile| %> <p>好きなアーティスト</p> <li><%= profile.artist %></li> <p>好きな曲</p> <li><%= profile.song %></li> <% end %>

users/controller.rb

def show @profiles = Profile.order(id: :desc) end

補足情報(FW/ツールのバージョンなど)

rails -v 5.2.4.3
cloud9

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

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

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

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

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

guest

回答1

0

ベストアンサー

current_user.build_profileでは新しく作ってしまうので

ruby

1def show 2 @user = User.find(params[:id]) 3 @profile = current_user.profile 4end

ですね

投稿2020/06/05 05:20

asm

総合スコア15147

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

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

huu

2020/06/05 07:35

教えて頂いたコードで解決できました。 ありがとうございます。助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問