前提・実現したいこと
scaffoldを使わずにCRUD機能をもつものを作成しています。
そこで<%= form.file_field :image %>で画像のアップロードをしようとしています。
ファイル名では反映されるのですが、画像として反映されず詰まっております。
分かる方がいらっしゃいましたらご教授いただければ幸いです。
発生している問題・エラーメッセージ
上記のイメージ画像内の「写真」の箇所には#ActionDispatch::Http::UploadedFile:………とファイル名なってしまいます。この部分をファイル名ではなく、上記のイメージ画像のように画像として「写真」の箇所に反映させたいです。
該当のソースコード
new.html.erb, edit.html.erb
<div class="field"> <%= f.label :image, t('.image') %> <%= f.file_field :image %> </div>
index.html.erb
<tbody> <% @lists.each do |list| %> <tr> <td><%= list.name %></td> <td><%= list.taste %></td> <td><%= list.price %></td> <td><%= list.service %></td> <td><%= list.memo %></td> <td><%= list.image.to_s %></td> <td><%= list.total %></td> <td><%= link_to t('.show'), list, class: 'btn btn-info' %></td> <td><%= link_to t('.edit'), edit_list_path(list), class: "btn-flat-border rounded-pill" %></td> <td><%= link_to t('.delete'), list_path(list), method: :delete, data: { confirm: '本当に削除しますか?' }, class: "btn-flat-border rounded-pill" %></td> </tr> <% end %> </tbody>
show.html.erb
<p> <strong><%= t('.image') %> :</strong> <%= @list.image.to_s %> </p>
lists.controller.rb
class ListsController < ApplicationController before_action :authenticate_user! def index @lists = List.all end def show @list = List.find(params[:id]) end def new @list = List.new end def create @list = List.create(list_params) @list.save redirect_to lists_path end def edit @list = List.find(params[:id]) end def update @list = List.find(params[:id]) @list.update(list_params) redirect_to lists_path end def destroy @list = List.find(params[:id]) @list.destroy redirect_to lists_path end private def list_params params.require(:list).permit(:name, :taste, :price, :service, :memo, :image, :total) end end
補足情報(FW/ツールのバージョンなど)
rails 5.2.3
回答2件
あなたの回答
tips
プレビュー