前提・実現したいこと
form_withで色々と入力してもらって, postとして別のユーザーに送信するアプリを開発中です. その中で, 返信機能を実装したいのですが, 返信用のform_withに予め入れておきたい箇所 があるのですが, それらをどのように入れればいいのかイマイチわかりません, 教えて下さい.
以下の例をあげると, 返信用ではタイトルの部分のみ入れ替えて表示して書き換えなくて済むようにしたいです.
ex)
タイトル: 不安なあなたへ
本文: 僕は明日試験の本番です. 僕も不安ですが頑張りましょう
送り主: 崖っぷち受験生 より
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
返信用
タイトル: 崖っぷち受験生へ
本文: 明日大事プレゼンがある者です. お互いに頑張りましょうね.
送り主: 不安な社会人 より
ソースコードのplaceholderに関しては, 参考までにという意味でユーザーに表示させています.
発生している問題・エラーメッセージ
一度form_withで受け取ったpostを, 返信用のpostに予め入れられるところを入れておきたいです.
該当のソースコード
new.html.erb
<div class="main posts-new"> <div class="container"> <h1 class="form-heading">手紙を書く</h1> <%= form_with scope: :post, url: posts_path, local: true do |form| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% @post.errors.full_messages.each do |msg|%> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="form"> <div class="form-body"> <%= form.label :宛名 %><br> <%= form.text_field :dear , placeholder: "どこかの誰かへ"%> <%= form.label :本文 %><br> <%= form.text_area :content %> <%= form.label :差出人 %><br> <%= form.text_field :from, placeholder: "試される大地の若者より"%> <%= form.submit value="投函する" %> </div> </div> <% end %> </div> </div>
schema.rb
create_table "posts", force: :cascade do |t| t.string "dear" t.text "content" t.string "from" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end
試したこと
もともとのform_withで入力された内容 (タイトル)をplaceholderないしはデフォルトで表示させられるのかなとも思ったのですが, 見つけられなかったので質問させていただきました.
posts_controller.rb
def reply @next_dear = Post.params[:from] @post = Post.new(post_params) @post.sender_id = @post.receiver_id @post.receiver_id = current_user.id @post.save! redirect_to posts_path end
reply.html.erb の一部
<div class="form"> <div class="form-body"> <%= form.label :宛名 %><br> <%= form.text_field :dear, value: @next_dear%> <%= form.label :本文 %><br> <%= form.text_area :content %> <%= form.label :差出人 %><br> <%= form.text_field :from, placeholder: "#{username}より"%> <%= form.submit value="投函する" %> </div> </div>
補足情報(FW/ツールのバージョンなど)
ruby 2.6.5
rails 6.0.3

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/27 08:22
2021/02/28 01:27
2021/03/01 04:22
2021/03/02 00:09 編集
2021/03/03 11:15