前提
投稿機能の実装
実現したいこと
保存できた場合とできなかった場合の
移動さきを指定
発生している問題・エラーメッセージ
新規投稿時保存されないときでもroot_pathに移動してしまう 編集後、詳細ページにいかず、root_pathに行ってしまう
該当のソースコード
PrototypesController
1class PrototypesController < ApplicationController 2 before_action :set_prototype, only: [:show, :edit] 3 def index 4 @user = current_user.name 5 @prototypes = Prototype.all 6 end 7 8 def new 9 @prototype = Prototype.new 10 end 11 12 def create 13 Prototype.create(prototype_params) 14 if 15 redirect_to root_path 16 else 17 render "form" 18 end 19end 20 21 def show 22 23 end 24 25 def edit 26 27 end 28 29 def update 30 @prototype = Prototype.find(params[:id]) 31 if @prototype.update(prototype_params) 32 redirect_to prototypes_path(@prototype.id) 33 else 34 render "edit" 35 end 36 end 37 38 39 private 40 def prototype_params 41 params.require(:prototype).permit(:title, :concept, :image, :catch_copy).merge(user_id: current_user.id) 42 end 43 def set_prototype 44 @prototype = Prototype.find(params[:id]) 45 end 46 end
_form.html.erb
1<%= form_with model: @prototype, local: true do |f|%> 2 <div class="field"> 3 <%= f.label :title, "プロトタイプの名称" %><br /> 4 <%= f.text_field :title, id:"prototype_title" %> 5 </div> 6 7 <div class="field"> 8 <%= f.label :catch_copy, "キャッチコピー" %><br /> 9 <%= f.text_area :catch_copy, class: :form__text, id:"prototype_catch_copy" %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :concept, "コンセプト" %><br /> 14 <%= f.text_area :concept, class: :form__text, id:"prototype_concept" %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :image, "プロトタイプの画像" %><br /> 19 <%= f.file_field :image, id:"prototype_image" %> 20 </div> 21 22 <div class="actions"> 23 <%= f.submit "保存する", class: :form__btn %> 24 </div> 25<% end %>
prototype.rb
1class Prototype < ApplicationRecord 2 belongs_to :user 3 has_many :comments 4 has_one_attached :image 5 6 validates :title, presence: true 7 validates :catch_copy, presence: true 8 validates :concept, presence: true 9 validates :image, presence: true 10end
### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/27 14:25
2022/11/27 14:27
2022/11/27 14:33
2022/11/27 14:37 編集