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

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

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

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

699閲覧

Rails AとBの入力画面で入力した文字をindexページに表示させたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2021/07/31 09:26

編集2021/08/01 07:27

間抜けな質問で申し訳ありません、一週間ほど色々と試行錯誤をしてみましたが、うまくできないため質問しました。

よくあるコメント機能を作りたいのですが、comments/new.html.erbで入力したものとtext/new.html.erbで入力したものをusers/index.html.erbに以下のように(A1)表示したいのですが、コードが悪いせいかうまく表示できません。


(A1)
comments/new.html.erbとtexts/new.html.erb両方にform_forで入力画面を作り
そこに文字を入力して、users/index.html.erbページに


comment.title
comment.body

text.title 1
text.body 1
...
text.title n個目
text.body n個目


という固まりとして入力した分だけ表示させたいです。


情報

Rails 5.1.7
VScode

model
:comment (title:string body:text)
:text (title:string body:text)
:devise users
views
:comments (index show new edit)
:text (index show new edit)
:users (index show edit)

tecxtcontroller

1class TextsController < ApplicationController 2 def index 3 @texts = Text.all 4 end 5 6 def show 7 @text = Text.find(params[:id]) 8 end 9 10 def new 11 @text = Text.new 12 end 13 14 def create 15 @text = Text.new(text_params) 16 @text.user_id = current_user.id 17 @text.save 18 redirect_to texts_path 19 end 20 21 def edit 22 @text = Text.find(params[:id]) 23 end 24 25 def update 26 @text = Text.find(params[:id]) 27 @text.update(text_params) 28 redirect_to text_path(@text) 29 end 30 31 def destroy 32 text = Text.find(params[:id]) 33 text.destroy 34 redirect_to texts_path 35 end 36 37 private 38 def text_params 39 params.require(:text).permit(:title, :body) 40 end 41end

commentscontroller

1class CommentsController < ApplicationController 2 def index 3 @comments = Comment.all 4 @texts = Text.all 5 end 6 7 def show 8 @comment = Comment.find(params[:id]) 9 end 10 11 def new 12 @comment = Comment.new 13 end 14 15 def create 16 @comment = Comment.new(comment_params) 17 @comment.user_id = current_user.id 18 @comment.save 19 redirect_to comments_path 20 end 21 22 def edit 23 @comment = Comment.find(params[:id]) 24 end 25 26 def update 27 @comment = Comment.find(params[:id]) 28 @comment.update(rblog_params) 29 redirect_to comment_path(@comment) 30 end 31 32 def destroy 33 comment = Comment.find(params[:id]) 34 comment.destroy 35 redirect_to comments_path 36 end 37 38 private 39 def comment_params 40 params.require(:comment).permit(:title, :body) 41 end 42end

userscontroller

1class UsersController < ApplicationController 2 def index 3 @users = User.all 4 @comments = Comment.all 5 @texts = Text.all 6 end 7 8 def show 9 @user = User.find(params[:id]) 10 end 11 12 def edit 13 @user = User.find(params[:id]) 14 end 15 16 def update 17 @user = User.find(params[:id]) 18 @user.update(user_params) 19 redirect_to user_path(@user) 20 end 21 22 private 23 def user_params 24 params.require(:user).permit(:username, :email, :profile) 25 end 26end 27

やったこと
このcomments/new.html.erbで入力したものと

rails

1comments/new.html.erb 2 3<%= form_for @comment do |f| %> 4 <p>title</p> 5 <%= f.text_field :title %> 6 <p>body</p> 7 <%= f.text_area :body %> 8 9 <%= f.submit "登録"%> 10<% end %> 11 12<script> 13$("textarea").val(""); 14 15</script>

同じように、texts/new.html.erbで入力したものを

rails

1texts/new.html.erb 2 3<%= form_for @text do |f| %> 4 <p>タイトル</p> 5 <%= f.text_field :title %> 6 <p>body</p> 7 <%= f.text_area :body %> 8 <%= f.submit "登録" %> 9<% end %> 10 11<script> 12$("textarea").val(""); 13</script>

↓users/index.html.erbとかくと

rails

1users/index.html.erb 2 3 <% @comments.each do |comment| %> 4 <div> 5 <%= comment.title %> 6 </div> 7 <div> 8 <%= comment.body %> 9 </div> 10 <% end %> 11 12 <% @texts.each do |text| %> 13 <div> 14 <%= text.title %> 15 </div> 16 <div> 17 <%= text.body%> 18 </div> 19 <% end %>

comment.title
comment.body

text.title
text.body

というふうに分かれてしまいます。


行ったこと
users/index.html.erbのcomments.each do |comment|の中に
textsのeach文を書きましたが

rails

1users/index.html.er 2 3<% @comments.each do |comment| %> 4 <div> 5 <div> 6 <%= comment.title %> 7 </div> 8 <div> 9 <%= comment.body %> 10 </div> 11 </div> 12 13 <% @texts.each do |text| %> 14 <div> 15 <div> 16 <%= text.title %> 17 </div> 18 <div> 19 <%= text.body%> 20 </div> 21 </div> 22 <% end %> 23<% end %> 24

これで表示させると、何個もtext.titleとtext.bodyが重複してしまいます。


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

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

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

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

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

guest

回答1

0

ベストアンサー

<% end %> <% @texts.each do |text| %>

の間の 空行 を削除してみて

投稿2021/08/01 06:13

winterboum

総合スコア23416

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

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

退会済みユーザー

退会済みユーザー

2021/08/01 07:25

わかりにくくてすいません!何回もアホな質問に答えていただきありがとうございます! 今回は comment.title comment.body text.title text.body 空白を消すのではなく --------------------------------------------- comment.title comment.body text.title 1 text.body 1 ... text.title n個目 text.body n個目 ーーーーーーーーーーーーーーーー 上のように、一個のcomment title bodyの要素の下に 加えて複数のtexts/new.html.erbで入力したものを 表示させたいです。 わかりにくくて申し訳ないです。
winterboum

2021/08/01 20:31

comment が3つtextが3つあるときにはどのようにしたいのか、書いてください
退会済みユーザー

退会済みユーザー

2021/08/02 02:08

--------------------------------------------- comment.title 1 comment.body 1 text.title A1 text.body A1 ... text.title An個目 text.body An個目 ーーーーーーーーーーーーーーーー --------------------------------------------- comment.title2 comment.body2 text.title B1 text.body B1 ... text.title Bn個目 text.body Bn個目 ーーーーーーーーーーーーーーーー --------------------------------------------- comment.title3 comment.body3 text.title C1 text.body C1 ... text.title Cn個目 text.body Cn個目 ーーーーーーーーーーーーーーーー といった具合に、一つのcomment.title/bodyに対して、複数のtext.newで入力したものを 表示させたいです。そういう場合はcomment textなどのように複数に入力するページを 分けない方がいいのでしょうか。わかりにくくて大変申し訳ありませんがよろしくお願いします。
winterboum

2021/08/02 02:55

model Co9mment と Text のcodeを載せてください
退会済みユーザー

退会済みユーザー

2021/08/02 04:26

models/cooment.rb class Comment < ApplicationRecord belongs_to :user end --------------------------------------------------- models/text.rb class Text < ApplicationRecord belongs_to :user 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 attachment :profile_image belongs_to :text has_many :comments, dependent: :destroy has_many :texts, dependent: :destroy end よろしくお願いします。
winterboum

2021/08/02 05:29

こういうのは 質問欄に <code>で載せてください。読みにくい
winterboum

2021/08/02 05:32

このような User、Comment、Text の関連ではお望みのはできません。 というのは User1 に Comment1,2 があり、Text 1,2,3,4 が有ったとき Comment1につけるのは Text1,2,3,4 のどれなのか、が判断付きません。 User 1:多 Comment、 Comment 1:多 Text って関係なのかな?
退会済みユーザー

退会済みユーザー

2021/08/02 07:07

お手数おかけし申し訳ありませんでした。
winterboum

2021/08/02 07:29

User 1:多 Comment、 Comment 1:多 Text って関係 にして、 Textの入力でCommentと関連つけるようにすればよいです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問