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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1230閲覧

rails 複数のshowページにnewで入力した同じものが反映される。

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/07/29 15:33

RubyOnRailsで小説投稿サイトを作っているのですが
nobel modelと(title:string body:textを持つ)nobelsというcontroller(index new show edit)に加え
title model(title:string synopsis:text)titlesというcontroller(index show edit new)を作成し

titles/new.html.erbでshowに文字を入力し、そのtitles/show.html.erbにnobels/new.html.erbで入力した文字を下記記載のtitles/show.html.erb4段目以降に反映させたいのですが

titles/new.html.erbで複数作成しても全部同じnobels/new.html.erbで作成したものになってしまいます。

↓このtitles/new submitで入力したものを
イメージ説明

↓titlesのshowにnobels/new.html.erbで入力したものを反映させていますが
![イメージ説明]

↓また、別のtitles/newでsubmitで入力したshowを見ると同じnobels/newで入力したものが
反映されてしまっています。(二つ目の画像とは違うtitles/show.html.erb)
![イメージ説明
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
例えばtitles/newで title:タイトル1と入力したshowページに
nobels/newで入力したものを別のtitle:タイトル1と入力したshow以外に反映させない
もしくは、nobels/newで作成したものを任意のshowに当てはめるためにはどうしたら良いでしょうか。
とてもわかりにくいですが、ご教授お願いいたします。

小説投稿サイトでよくある、ユーザーが小説全部を格納するページを作り、そこに入力したものを
入れたいというイメージです。

Rails 5.1.7のバージョンになります。

下記は実際に書いたコードになります。

ruby

1nobels/new.html.erb 2 3<%= form_for @nobel do |f| %> 4 <%= f.label :title %> 5 <%= f.text_field :title %> 6 7 <%= f.label :body %> 8 <%= f.text_area :body %> 9 10 <%= f.submit "登録" %> 11<% end %>

ruby

1title/new.html.erb   2<%= form_for @title do |f| %> 3 <%= f.label :title %> 4 <%= f.text_field :title %> 5 6 <%= f.label :あらすじ %> 7 <%= f.text_area :synopsis %> 8 9 <%= f.submit "登録" %> 10 11<% end %>

ruby

1titles/show.html.erb 2 3<%= @title.title %> 4<%= @title.synopsis %> 5<% @nobels.each do |nobel| %> 6 <%= nobel.title %> 7<% end %> 8

ruby

1titles/show.html.erb 2<div> 3<%= @title.title %> 4</div> 5<div> 6<%= @title.synopsis %> 7</div> 8<% @nobels.each do |nobel| %> 9<div> 10<p>nobels/new/html.erbで入力したもの</p> 11</div> 12<div> 13 <%= nobel.title %> 14 </div> 15 <div> 16 <%= nobel.body %> 17 </div> 18<% end %> 19

ruby

1title model 2 3def index 4 @titles = Title.all 5 end 6 7 def show 8 @title = Title.find(params[:id]) 9 @nobels = Nobel.all 10 end 11 12 def edit 13 @title = Title.find(params[:id]) 14 end 15 16 def create 17 @title = Title.new(title_params) 18 @title.user_id = current_user.id 19 @title.save 20 redirect_to title_path(@title) 21 end 22 23 def new 24 @title = Title.new 25 end 26 27 def update 28 @title = Title.find(params[:id]) 29 @title.update(title_params) 30 redirect_to title_path(@title) 31 end 32 33 def destroy 34 title = Title.find(params[:id]) 35 title.destroy 36 redirect_to title_path 37 end 38 39 40 private 41 def title_params 42 params.require(:title).permit(:title, :synopsis) 43 end 44end 45

ruby

1nobel model 2def index 3 @nobels = Nobel.all 4 end 5 6 def show 7 @nobel = Nobel.find(params[:id]) 8 end 9 10 def edit 11 @nobel = Nobel.find(params[:id]) 12 end 13 14 def create 15 @nobel = Nobel.new(nobel_params) 16 @nobel.user_id = current_user.id 17 @nobel.save 18 redirect_to nobel_path(@nobel) 19 end 20 21 def new 22 @nobel = Nobel.new 23 end 24 25 def update 26 @nobel = Nobel.find(params[:id]) 27 @nobel.update(nobel_params) 28 redirect_to title_path(@title) 29 end 30 31 def destroy 32 nobel = Nobel.find(params[:id]) 33 nobel.destroy 34 redirect_to nobel_path 35 end 36 37 38 private 39 def nobel_params 40 params.require(:nobel).permit(:title, :body, :comment) 41 end 42end

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

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

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

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

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

guest

回答1

0

ベストアンサー

Jqueryでnobel/newをcreateした後に要素を空にしたらいけました。

投稿2021/07/30 15:25

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問