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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Q&A

解決済

1回答

827閲覧

rails AgumentErrorを解決したいです

Jin12

総合スコア1

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/10/04 05:31

現在写真を投稿できるアプリを実装しているのですが投稿の詳細ページから編集ページに遷移した時に画像の情報が保持されません。
他の説明文などは保持されいるのですが画像のみ消えている状態です。

show.html.erb

ruby

1<%= render "shared/header" %> 2 3<div class="posts"> 4 <div class="post"> 5 <div class="top-post"> 6 <%= link_to "投稿者 #{@post.user.nickname}", user_path(@post.user.id), class:"user-name-post" %> 7 <span><%= @post.created_at.to_s(:datetime_jp) %></span> 8 </div> 9 <div class="main-post"> 10 <div class="trash-edit"> 11 <div class="trash"> 12 <%= link_to(root_path,class:"fa fa-trash fa-4x trash-btn",style: "display: flex;" ) do%> 13 <% end %> 14 </div> 15 <div class="exit"> 16 <%= link_to(edit_post_path(@post.id),class:"fa fa-edit fa-4x edit-btn",style: "display: flex;") do %> 17 <% end %> 18 </div> 19 </div> 20 <%= link_to(post_path(@post.id)) do %> 21 <%= image_tag @post.image, class: 'image-main-post' %> 22 <% end %> 23 </div> 24 <div class="footer-post"> 25 <%= link_to @post.user.nickname, user_path(@post.user.id), class:"user-name-post" %> 26 <p><%= @post.caption %></p> 27 </div> 28 </div> 29<div> 30<%= render "shared/footer" %>

edit.html.erb

ruby

1<%= render "shared/header" %> 2 3 4<div class="new-post"> 5 <%= form_with model: @post, class: 'form', local: true do |f| %> 6 <div class="edit-page"> 7 <h2>Edit Page</h2> 8 </div> 9 <%= render 'shared/error_messages', model: f.object %> 10 <div class="image-post"> 11 <%= f.file_field :image, class: "file-post" %> 12 </div> 13 <div class="caption-post"> 14 <%= f.text_area :caption, class:"text-post", placeholder:"投稿するアイテムの口コミを書こう!" ,rows:"7" ,maxlength:"1000" %> 15 </div> 16 <div class="category"> 17 <p>カテゴリー</p> 18 </div> 19 <div class="category-post"> 20 <%=f.collection_select(:category_id, Category.all, :id, :name, {}, {class:"collection-post"}) %> 21 </div> 22 <div class="post-submit"> 23 <%= f.submit 'SEND', class: 'submit' %> 24 </div> 25 <% end %> 26</div> 27 28<%= render "shared/footer" %>

posts.controller.rb

ruby

1class PostsController < ApplicationController 2 before_action :authenticate_user!, only: :new 3 4 def index 5 @posts = Post.all.order("created_at DESC") 6 end 7 8 def new 9 @post = Post.new 10 end 11 12 def create 13 @post = Post.new(post_params) 14 if @post.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def show 22 @post = Post.find(params[:id]) 23 end 24 25 def edit 26 @post = Post.find(params[:id]) 27 end 28 29 def update 30 @post = Post.new(post_params) 31 if @post.save 32 redirect_to post_path(@post.id) 33 else 34 render :edit 35 end 36 end 37 38 private 39 40 def post_params 41 params.require(:post).permit(:image,:caption,:category_id).merge(user_id: current_user.id) 42 end 43end

models/post.rb

ruby

1class Post < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 belongs_to :category 4 5 belongs_to :user 6 has_one_attached :image 7 8 with_options presence: true do 9 validates :caption 10 validates :category_id, numericality: {other_than: 1, message: "can't be blank"} 11 end 12end

画像はActiveStorageを使っています。
そもそも詳細ページから編集ページに遷移する時に画像の情報は消えてしまうものなのでしょうか?
どなたか分かる方いればよろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

f.file_field はimageを表示する機能はありませんから、表示させるとしたら、 f.file_field のあたりに
<%= image_tag @post.image, class: 'image-main-post' if @post.image %>
とでもする必要があります。

投稿2021/10/04 08:20

winterboum

総合スコア23347

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

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

Jin12

2021/10/04 11:20

ありがとうございました! f.file_fieldはformで使う時のものでimage.tagがimageを表示させるものという事ですね!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問