前提・実現したいこと
ここに質問の内容を詳しく書いてください。
# Railsにて複数画像の投稿を実装したいのですが上手くいきません。
発生している問題・エラーメッセージ
undefined method `image' for
該当のソースコード
form
1<%= form_with model: @item, local: true do |f|%> 2 <div class="field"> 3 <%= f.label :manufacturer, "メーカー名", class: :form_name %><br /> 4 <%= f.text_field :manufacturer, class: :form_text, id:"item_manufacturer" %> 5 </div> 6 7 <div class="field"> 8 <%= f.label :item_name, "商品名", class: :form_name %><br /> 9 <%= f.text_area :item_name, class: :form_text, id:"new_item_name" %> 10 </div> 11 12 <div class="field"> 13 <%= f.label :standard, "規格", class: :form_name %><br /> 14 <%= f.text_area :standard, class: :form_text, id:"item_standard" %> 15 </div> 16 17 <div class="field"> 18 <%= f.label :storage_condition, "保存方法", class: :form_name %><br /> 19 <%= f.text_area :storage_condition, class: :form_text, id:"item_storage_condition" %> 20 </div> 21 22 <div class="field"> 23 <%= f.label :description, "商品説明", class: :form_name %><br /> 24 <%= f.text_area :description, class: :form__text, id:"item_description" %> 25 </div> 26 27 <div class="field"> 28 <%= f.label :image, "新商品の画像", class: :form_name %><br /> 29 <%= f.file_field :images, name: 'item[images][]', id:"item_image", class: :form_name %> 30 </div> 31 32 <div class="actions"> 33 <%= f.submit "保存する", class: :send, class: :form_name %> 34 </div> 35 <div id="image-list" ></div> 36<% end %> 37 38<% @item.images.each do |image| %> 39 <%= image_tag image, class: 'images' %> 40<% end %>
index
1 <%# 商品一覧 %> 2 <div class='item-contents'> 3 <ul class='item-lists'> 4 5 <% @items.each do |item| %> 6 <li class='list'> 7 <%#<%= link_to items_path(item.id), method: :get do %> 8 <div class='item-img-content'> 9 <% item.images. each do |image| %> 10 <%= image_tag image, class: "item-img"%> 11 <% end %> 12 </div> 13 <%#<% end %> 14 <div class='item-info'> 15 <h3 class='item_name'> 16 <p><%= link_to item.manufacturer, item_path(item.id), method: :get, class: "item_name2" %></p> 17 <p><%= link_to item.item_name, item_path(item.id), method: :get, class: "item_name2" %></p> 18 <p><%= link_to "詳細表示", item_path(item.id), method: :get, class: "btn btn-secondary" %></p> 19 </h3> 20 </div> 21 <% end %> 22 </li> 23 </ul> 24 </div> 25 <%# /商品一覧 %> 26 </body> 27 28</main> 29 30<footer class="container"> 31 <p class="float-end"><a href="/">トップに戻る</a></p> 32 <p>© 2017-2021 Company, Inc. · <a href="#">プライバシー</a> · <a href="#">利用規約</a></p> 33</footer> 34
show
1<main class="item-main"> 2 <div class="item-inner"> 3 <div class="item__wrapper"> 4 <p class="item__hedding"> 5 <%= @item.item_name %> 6 </p> 7 <% if user_signed_in? && current_user.id == @item.user_id %> 8 <div class="item__manage"> 9 <%= link_to "編集する", edit_item_path(@item.id),method: :get, class: :prototype__btn %> 10 <%= link_to "削除する", item_path(@item.id),method: :delete, class: :prototype__btn %> 11 </div> 12 <% end %> 13 <div class="item__image"> 14 <% item.images.each do |image| %> 15 <%= image_tag image, class:'image' %> 16 <% end %> 17 <%= link_to 'サンプル依頼する', new_contact_path, method: :get, class: "btn btn-outline-danger" %> 18 </div> 19 <table class="table table-bordered"> 20 <thead> 21 <tr> 22 <th scope="col">メーカー名</th> 23 <th scope="col">商品名 </th> 24 <th scope="col">商品規格 </th> 25 <th scope="col">温度帯 </th> 26 </tr> 27 </thead> 28 <tbody> 29 <tr> 30 <th scope="row"><%= @item.manufacturer %></th> 31 <td><%= @item.item_name %></td> 32 <td><%= @item.standard %></td> 33 <td><%= @item.storage_condition %></td> 34 </tr> 35 </tbody> 36 </table> 37 <div class="item-description"> 38 <div class="item-discription-name"> 39 <%= '《商品説明》' %> 40 <div style="padding: 10px; margin-bottom: 10px; border: 1px solid #333333;"> 41 <%= @item.description %> 42 </div> 43 </div> 44 </div> 45 </div> 46 </div>
new
1<div class="navbar navbar-dark bg-dark shadow-sm"> 2 <div class="container"> 3 <a href="#" class="navbar-brand d-flex align-items-center"> 4 <strong>N_Web_Apprication</strong> 5 </a> 6 <div class="d-flex"> 7 <%= link_to "トップへ", root_path, class: "top_button" %> 8 </div> 9 </div> 10</div> 11 12<div class='registration-main'> 13 <div class='form-wrap'> 14 <div class="form__wrapper"> 15 <h2 class="page-heading"> 新商品の投稿 </h2> 16 <%= render partial: 'form' %> 17 </div> 18 </div> 19</div> 20<%= render "shared/second-footer" %> 21
controller
1class ItemsController < ApplicationController 2 before_action :authenticate_user! 3 before_action :set_item, only: [:show, :edit, :update, :destroy] 4 5 def index 6 @items = Item.all 7 end 8 9 def new 10 @item = Item.new 11 end 12 13 def create 14 @item = Item.new(item_params) 15 if @item.save 16 redirect_to root_path 17 else 18 render :new 19 end 20 end 21 22 def show 23 end 24 25 def edit 26 end 27 28 def update 29 if @item.update(item_params) 30 redirect_to item_path 31 else 32 render :edit 33 end 34 end 35 36 def destroy 37 if @item.destroy 38 redirect_to action: :index 39 else 40 render :show 41 end 42 end 43 44 45 private 46 47 def item_params 48 params.require(:item).permit(:manufacturer, :item_name, :standard, :storage_condition, :sell_by, :description, images: []).merge(user_id: current_user.id) 49 end 50 51 def set_item 52 @item = Item.find(params[:id]) 53 end 54 55end 56
model
1class Item < ApplicationRecord 2 3 with_options presence: true do 4 validates :manufacturer 5 validates :item_name 6 validates :standard 7 validates :storage_condition 8 validates :description 9 validates :image 10 validates :user_id 11 end 12 13 belongs_to :user 14 has_many_attached :images 15 16end 17
試したこと
paramsの書き方やveiwの表示方法等色々参考にさせてもらいましたが解決できませんでした。
よろしくお願いします。
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー