投稿画面から確認画面を経由しての画像アップロードに関して
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 1,011
前提・実現したいこと
画像アップロードに確認画面を挟んで行いたいのですが、
確認画面までは表示できているのですが、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
よろしくお願いいたします。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
check解決した方法
0
こちらヘルパーメゾットでchoose_new_or_editを修正したらうまくきました。
posts_pathにつながっていなかったみたいです。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 89.97%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる