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

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

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

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

711閲覧

複数画像の実装にてundefind methodが解決したい

kenta34344

総合スコア5

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2021/07/28 09:57

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
# 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>&copy; 2017-2021 Company, Inc. &middot; <a href="#">プライバシー</a> &middot; <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の表示方法等色々参考にさせてもらいましたが解決できませんでした。
よろしくお願いします。

ここにより詳細な情報を記載してください。

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

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

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

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

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

winterboum

2021/07/28 11:55

エラーメッセージは省略しないで
kenta34344

2021/07/28 21:00

すみませんでした。 NoMethodError in ItemsController#create undefined method `image' for #<Item:0x00007fef5d4dd858> Did you mean? images images= です。よろしくお願いします。
winterboum

2021/07/28 22:54

質問欄に載せてね。行数の表示はなかったのですね?
guest

回答1

0

ベストアンサー

has_many_attached :images とあるのですから、 image ではなく images ですね

投稿2021/07/28 22:58

winterboum

総合スコア23416

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

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

kenta34344

2021/07/29 03:50

すみません。 imageではなくimagesになるのは、どの部分でしょうか?
winterboum

2021/07/29 05:21

NoMethodError in ItemsController#create undefined method `image' for #<Item とありますから、createのなかでitem に imageしている所を探しましょう
kenta34344

2021/07/29 11:18

返信ありがとうございます。 createの中でitemにimageしている箇所は、newからformで送られる内容とparamsの記述部分でよろしいでしょうか? <%= f.file_field :images, name: 'item[images][]', id:"item_image", class: :form_name %>のimageをimagesに変更しましたが変化はありませんでした。他のviewなんかも影響してきますか?
winterboum

2021/07/29 12:17

変化はありません というのは undefined method `image' for #<Item のままということですか?
kenta34344

2021/07/29 12:33

NoMethodError in ItemsController#create undefined method `image' for #<Item:0x00007fd99cc06390> Did you mean? images images= Extracted source (around line #15): 13 def create 14 @item = Item.new(item_params) 15 if @item.save 16 redirect_to root_path 17 else 18 render :new の15行目でエラーが出ています。
winterboum

2021/07/29 20:16

ということは、まだ「images にすべき image」が残っているということです item_params は質問欄に載っているとおりですか?
kenta34344

2021/07/29 21:27

item_paramsは質問欄のままです。 他のimageを探してみます。
kenta34344

2021/07/30 11:27

item.rbのvalidates :imageをimagesにする事で表示できました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問