前提・実現したいこと
画像アップロードに確認画面を挟んで行いたいのですが、
確認画面までは表示できているのですが、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
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。