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

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

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

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

Q&A

1回答

728閲覧

保存した画像を編集画面で表示させたい。

akito_tl

総合スコア2

Ruby on Rails

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

0グッド

0クリップ

投稿2020/09/16 14:16

編集2020/09/16 23:12

#質問
投稿した画像を編集画面で入れ替えることができる機能を実装したいのですが、
下記のエラーが表示され、上手くデータベースから取り出すことができません。。。
(画像のみfields_forを使用しimageテーブルへ保存し、他のデータはitemテーブルへ保存しております)
string型を変更したりと色々試したのですが、原因が全く掴めません。
ぜひ本エラーに関してご教授いただけたらと思います。宜しくお願い致します。

#作業手順
1,投稿機能の実装(new.html.haml)完了後に編集ページ(edit.html.haml)を作成。
2,items_controllerにset_itemの部分を記述。
3,作成したedit.html.hamlページへnew.html.hamlの内容をコピペし、ページへ遷移すると下記のエラーが発生。

#エラー
app/views/items/edit.html.haml where line #19 raised:

undefined method `url' for "#ActionDispatch::Http::UploadedFile:0x00007fd2eb425c50":String
= image_tag image.src.url, data: { index: i }, width: "100", height: '100'

#コード

edit.html.haml

.items = form_with model: @item,local: true do |f| .items__box .items__box--list %p.name 出品画像 %p.need 必須 .items__box--image #previews - if @item.persisted? - @item.images.each_with_index do |image, i| = image_tag image.src.url, data: { index: i }, width: "100", height: '100' = f.fields_for :images,@item.images.build do |image| .js-file_group{"data-index" => "#{image.index}"} .image-file = image.file_field :src,class: 'js-file' %span.js-remove 削除 - if @item.persisted? = image.check_box :_destroy, data:{ index: image.index }, class: 'hidden-destroy' - if @item.persisted? .js-file_group{"data-index" => "#{@item.images.count}"} = file_field_tag :src, name: "item[images_attributes][#{@item.images.count}][]", class: 'js-file' .js-remove 削除 以下省略

items_controller

class ItemsController < ApplicationController before_action :set_category, only: [:new, :edit, :create, :update, :destroy] before_action :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy] before_action :set_item, except: [:index, :new, :create] def get_category_children @category_children = Category.find(params[:parent_name]).children end def get_category_grandchildren @category_grandchildren = Category.find("#{params[:child_id]}").children end def index @items = Item.includes(:images).order('created_at DESC') end def new @item = Item.new @item.images.new end def show end def create @item = Item.new(item_params) if @item.save redirect_to root_path else render :new end end def edit @grandchild_category = @item.category @child_category = @grandchild_category.parent @category_parent = @child_category.parent @category = Category.find(params[:id]) @category_children = @item.category.parent.parent.children @category_grandchildren = @item.category.parent.children end private def item_params params.require(:item).permit(:name, :description,:brand,:condition_id,:shipping_area_id,:shipping_method_id,:shipping_burden_id,:category_id,:seller_id,:buyer_id,:shipping_data,:price,images_attributes: [:src, :_destroy, :id]).merge(seller_id: current_user.id) end def set_category @category_parent_array = Category.where(ancestry: nil) end def set_item @item = Item.find(params[:id]) end end

item.rb

class Item < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions has_many :images accepts_nested_attributes_for :images belongs_to :seller,class_name: "User",foreign_key: "seller_id",optional: true belongs_to :buyer, class_name: "User", foreign_key: "buyer_id",optional: true validates :name,presence: true, length:{maximum: 6} end

image.rb

class Image < ApplicationRecord mount_uploader :src, ImageUploader belongs_to :item end

imageのマイグレーションファイル

class CreateImages < ActiveRecord::Migration[6.0] def change create_table :images do |t| t.string :src t.references :item, foreign_key: true t.timestamps end end end

itemのマイグレーションファイル

class CreateItems < ActiveRecord::Migration[6.0] def change create_table :items do |t| t.string :name, null: false t.text :description, null:false t.string :brand t.string :condition_id, null: false t.string :shipping_burden_id t.string :shipping_method_id, null: false t.string :shipping_area_id, null: false t.string :category_id, null: false t.integer :shipping_data, null: false t.integer :price, null: false t.references :seller, foreign_key: {to_table: :users} t.references :buyer, foreign_key: {to_table: :users} t.timestamps end end end

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

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

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

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

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

winterboum

2020/09/16 22:48

どのタイミングでそのエラーが出たのかがわかりません。手順を載せてください 19行目ってどの行でしょう
akito_tl

2020/09/16 23:14

コメントありがとうございます。 19行目は、edit.html.hamlの= image_tag image.src.url, data: { index: i }, width: "100", height: '100'という部分になります。 手順に関しても追記させていただきました。 至らぬ部分が多くご迷惑おかけしますが、何卒宜しくお願い致します。
guest

回答1

0

image.src.url の image.src が Stringだと言われています。
が、
codeをみると正しそう。はて、、、
show では表示できていますか?

投稿2020/09/16 23:23

winterboum

総合スコア23329

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

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

akito_tl

2020/09/17 12:03

showでも表示できない状況です。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問