Q&A
いつもお世話になっております。
ユーザーに紐づいているコメントを投稿する際に
ユーザーの一部のレコードも一緒に編集できるようにしたいと考えています。
具体的には、コメントを投稿する際にユーザーの電話番号を編集できるようにするようなイメージです。
エラー
Ruby
1Couldn't find User with ID=2 for Tweet with ID= 2 3 def create 4 @tweet = current_user.tweets.create(create_params) 5 if @tweet.save 6 flash[:success] = "Tweet created!" 7 redirect_to root_path
該当するソースコード
Tweetsコントローラー
Ruby
1class TweetsController < ApplicationController 2 3 def new 4 if signed_in? 5 @tweet = current_user.tweets.build 6 else 7 redirect_to new_user_registration_path 8 end 9 end 10 11 def show 12 end 13 14 def create 15 @tweet = current_user.tweets.create(create_params) 16 if @tweet.save 17 flash[:success] = "Tweet created!" 18 redirect_to root_path 19 else 20 flash[:alert] = "I'm sorry, error occured." 21 redirect_to root_path 22 end 23 end 24 25 def create_params 26 params.require(:tweet).permit(:id, :contentuser_attributes: [:id, :phone]) 27 end 28end
view
Ruby
1 <%= form_for(@tweet) do |f| %> 2 <%= f.fields_for :user do |i| %> 3 <div class="form-group"> 4 <label>Phone number</label> 5 <%= i.text_field :phone, class: "form-control", autofocus: true %> 6 </div> 7 <% end %> 8 <div class="form-group"> 9 <label>Content</label> 10 <%= f.text_field :content, class: "form-control", autofocus: true %> 11 </div> 12
すみませんが、どうぞよろしくお願いいたします。
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2017/11/20 14:53