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

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

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

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

Q&A

解決済

1回答

3145閲覧

投稿画面から確認画面を経由しての画像アップロードに関して

Malas

総合スコア112

Ruby on Rails

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

0グッド

0クリップ

投稿2018/09/15 01:38

前提・実現したいこと

画像アップロードに確認画面を挟んで行いたいのですが、
確認画面までは表示できているのですが、show.html.erbで表示することができなくて困っております。
アドバイスをいただけるとたすかります。

発生している問題・エラーメッセージ

confirm.html.erbまで画像を表示できているのにshow.html.erbでは表示できない。

_form.html.erb

<%= form_with(model: @post, local: true ,url: choose_new_or_edit ) 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> <%= image_tag(@post.image) if @post.image %> <%= f.file_field :image, id: :post_image %> <%= text_area_tag :content, @post.content %> <input type="submit" value="投稿を確認" class="btn btn-link"> </div> </div> <% end %>

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, multipart: 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,:size =>'350x200') %> <%= f.hidden_field :image_cache %> </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"> <%= f.hidden_field :content %> <%= f.hidden_field :image_cache %> <%= submit_tag '戻る', name: 'back',class: "btn btn-link" %> </div> </div> </div>   <% end %>  </div> </did>

show.html.erb

<div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <div class="post-user-name"> <%= image_tag @user.image %> <%= link_to @user.name, user_path(@user.id) %> </div> <%= image_tag @post.image, :size =>'350x200' %> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at %> </div> <% unless @post.user_id == current_user.id %> <% if @favorite.present? %> <%= link_to 'お気に入りから外す', favorite_path(id: @favorite.id), method: :delete %> <span class="fa fa-star notfavorite-btn"></span> <% else %> <%= link_to 'お気に入りに追加', favorites_path(post_id: @post.id), method: :post %> <span class="fa fa-star favorite-btn"></span> <% end %> <% end %> <%= @favorites_count %> <% if @post.user_id == @current_user.id %> <div class="post-menus"> <%= link_to '編集', edit_post_path(@post.id) %> <%= link_to '削除', post_path(@post.id), {method: "DELETE"} %> </div> <% end %> </div> </div> </div>

posts_controller

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.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 @post = Post.new(post_params) @post.content = params[:content] 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 private def post_params params.require(:post).permit(:image, :image_cache, :content) end def set_post @post = Post.find(params[:id]) end end

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

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

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

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

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

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

guest

回答1

0

自己解決

こちらヘルパーメゾットでchoose_new_or_editを修正したらうまくきました。
posts_pathにつながっていなかったみたいです。

投稿2018/09/17 10:13

Malas

総合スコア112

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問