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

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

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

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

Q&A

解決済

1回答

1282閲覧

confirmアクションを経由せずにconfirm.html.erbを表示してしまっている

Malas

総合スコア112

Ruby on Rails

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

0グッド

0クリップ

投稿2018/09/12 13:52

編集2018/09/12 13:53

前提・実現したいこと

railsでtwitterのようなSNSサイトを作成しており、
ユーザーが投稿した時に
投稿画面(new.html.erb) => 確認画面(confirm.html.erb) => 完了画面(show.html.erb)
という流れで投稿を行っており、動作自体は問題ないのですが
posts_controllerのconfirmアクションを経由せずに確認画面にいってしまいます。
アドバイスをいただけると助かります。

new.html.erb

<div class="main posts-new"> <div class="container"> <h1 class="form-heading">投稿する</h1> <%= form_with(model: @post, local: true, multipart: true) do |f| %> <div class="form"> <div class="form-body"> <% @post.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <p>画像を投稿</p> <%= f.file_field :image %> <%= f.hidden_field :image_cache %> <%= text_area_tag :content, @post.content %> <input type="submit" value="投稿を確認" class="btn btn-link"> </div> </div> <% end %> </div> </div>

confirm.html.erb

<div class="main posts-new"> <div class="container"> <h1 class="form-heading">投稿内容を確認してください</h1> <%= form_with(model: @post, url: posts_path, local: true) do |f| %> <div class="form"> <div class="form-body"> <div class="field"> <%= @post.content %> <%= hidden_field_tag :content, @post.content %> </div> <div class="field"> <%= image_tag(@post.image) %> <%= hidden_field_tag :"cache[image]", @post.image.cache_name %> </div> <div class="actions"> <%= submit_tag '投稿する', name: 'send',class: "btn btn-link"%> </div> <% end %> <%= form_with(model: @post, url:new_post_path, local:true, method:"get") do |f| %> <div class="actions"> <%= submit_tag '戻る', name: 'back',class: "btn btn-link" %> </div> </div> </div>   <% end %>  </div> </did>

_form.html.erb

<%= form_with(model: @post, local: true, url: confirm_posts_path) do |f| %> <div class="field"> <%= image_tag(@post.image.url) if @post.image.url %> <%= f.file_field :image, id: :post_image %> </div> <div class="field"> <%= f.label :content %><br> <%= f.text_area :content %> </div> <div class="actions"> <%= f.submit '投稿する' %> </div> <% end %>

posts_controller.rb

class PostsController < ApplicationController before_action :authenticate_user, only: [:new, :show, :edit] def index @posts = Post.all.order(created_at: :desc) end def show @post = Post.find_by(id: params[:id]) @favorite = current_user.favorites.find_by(post_id: @post.id) @user = User.find_by(id: @post.user_id) end def new if params[:back] @post = Post.new(post_params) else @post = Post.new end end def create @post = Post.new(content: params[:content],user_id: @current_user.id) @post.user_id = current_user.id if params[:back] render :new return end unless @post.valid? render :new return end if params[:send] @post.image.cache! unless @post.image.blank? @post.save PostMailer.post_mail(@post).deliver flash[:notice] = "投稿を作成しました" redirect_to @post return end render :confirm end def edit @post = Post.find_by(id: params[:id]) end def update @post = Post.find_by(id: params[:id]) @post.content = params[:content] if @post.save flash[:notice] = "投稿を編集しました" redirect_to posts_path else render :edit end end def destroy @post = Post.find_by(id: params[:id]) @post.destroy flash[:notice] = "投稿を削除しました" redirect_to posts_path end def confirm binding.pry @post = Post.new(post_params) @post.image.cache! unless @post.image.blank? if @post.invalid? render :confirm else render :posts end end def ensure_correct_user @post = Post.find_by(id: params[:id]) if @post.user_id != @current_user.id flash[:notice] = "投稿者が削除できます" redirect_to posts_path end end def post_params params.require(:post).permit(:image, :image_cache) end def set_post @post = Post.find(params[:id]) end end

試したこと

def createのrender :confirmの場所がおかしいのではないかと思っております。

よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

render :confirmにおける`:confirmは、描画するview(app/views/posts/confirm.html.erb)を指定しているのであって
コントローラーのアクション(PostsController#confirm)を指定しているわけではないです。

アクションを呼びたいのだったら、renderではなく単に

ruby

1def create 2 ... 3 ... 4 confirm 5end

とすればよいかと思います。

投稿2018/09/13 00:20

takumiabe

総合スコア661

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

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

Malas

2018/09/13 01:11

ありがとうございます。 書き換えたらconfirmアクションを経由しました。 しかし、confirm.html.erbで投稿内容が表示されず、投稿自体もできなくなりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問