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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1212閲覧

画像アップロードができない

yamada_yuuki

総合スコア100

Ruby

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

Ruby on Rails

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

0グッド

1クリップ

投稿2019/10/20 07:57

https://www.sejuku.net/blog/65175のサイトをもとに、画像アップロード機能を実装していたらエラーが出てしまいました。

ActionView::Template::Error (Can't resolve image into URL: undefined method `to_model' for #<AvatarUploader:0x000000000bf84de0> Did you mean? to_xml): 11: </p> 12: 13: <p> 14: <%= image_tag @yuuzi.avatar_path %> 15: </p> 16: 17: <%= link_to 'Edit', edit_yuuzi_path(@yuuzi) %> | app/views/yuuzis/show.html.erb:14:in `_app_views_yuuzis_show_html_erb__557274456_96452760'

controller

1class YuuzisController < ApplicationController 2 before_action :set_yuuzi, only: [:show, :edit, :update, :destroy] 3 4 # GET /yuuzis 5 # GET /yuuzis.json 6 def index 7 @yuuzis = Yuuzi.all 8 end 9 10 # GET /yuuzis/1 11 # GET /yuuzis/1.json 12 def show 13 end 14 15 # GET /yuuzis/new 16 def new 17 @yuuzi = Yuuzi.new 18 end 19 20 # GET /yuuzis/1/edit 21 def edit 22 end 23 24 # POST /yuuzis 25 # POST /yuuzis.json 26 def create 27 @yuuzi = Yuuzi.new(yuuzi_params) 28 29 respond_to do |format| 30 if @yuuzi.save 31 format.html { redirect_to @yuuzi, notice: 'Yuuzi was successfully created.' } 32 format.json { render :show, status: :created, location: @yuuzi } 33 else 34 format.html { render :new } 35 format.json { render json: @yuuzi.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 # PATCH/PUT /yuuzis/1 41 # PATCH/PUT /yuuzis/1.json 42 def update 43 respond_to do |format| 44 if @yuuzi.update(yuuzi_params) 45 format.html { redirect_to @yuuzi, notice: 'Yuuzi was successfully updated.' } 46 format.json { render :show, status: :ok, location: @yuuzi } 47 else 48 format.html { render :edit } 49 format.json { render json: @yuuzi.errors, status: :unprocessable_entity } 50 end 51 end 52 end 53 54 # DELETE /yuuzis/1 55 # DELETE /yuuzis/1.json 56 def destroy 57 @yuuzi.destroy 58 respond_to do |format| 59 format.html { redirect_to yuuzis_url, notice: 'Yuuzi was successfully destroyed.' } 60 format.json { head :no_content } 61 end 62 end 63 64 private 65 # Use callbacks to share common setup or constraints between actions. 66 def set_yuuzi 67 @yuuzi = Yuuzi.find(params[:id]) 68 end 69 70 # Never trust parameters from the scary internet, only allow the white list through. 71 def yuuzi_params 72 params.require(:yuuzi).permit(:name, :avatar_path) 73 end 74end 75

show

1<p id="notice"><%= notice %></p> 2 3<p> 4 <strong>Name:</strong> 5 <%= @yuuzi.name %> 6</p> 7 8<p> 9 <strong>Avatar path:</strong> 10 <%= @yuuzi.avatar_path %> 11</p> 12 13<p> 14<%= image_tag @yuuzi.avatar_path %> 15</p> 16 17<%= link_to 'Edit', edit_yuuzi_path(@yuuzi) %> | 18<%= link_to 'Back', yuuzis_path %>

初心者なので優しく答えていただけると嬉しいです。お願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

@yuuzi.avatar_path が AvatarUploader のオブジェクトになっているので、image_tagで解釈できるようにURLの文字列として渡してあげる必要がありますね。

https://github.com/carrierwaveuploader/carrierwave
公式ドキュメントではurlというメソッドが用意されているので @yuuzi.avatar_path.url で表示されるようになると思います。

投稿2019/10/21 09:48

8zca

総合スコア559

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

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

yamada_yuuki

2019/10/21 11:18

回答ありがとうございます。エラーは出なくなったんですが、画像が表示されません。画像フォルダの位置が表示されるだけになりました。どうしたら良いでしょうか。
8zca

2019/10/21 12:05

image_tag にわたす引数という意味です。 <%=image_tag @yuuzi.avatar_path.url %>
yamada_yuuki

2019/10/21 12:41

<p id="notice"><%= notice %></p> <p> <strong>Name:</strong> <%= @yuuzi.name %> </p> <p> <strong>Avatar path:</strong> <%= @yuuzi.avatar_path.url %> </p> <%= @yuuzi.avatar_path.url %> <%= link_to 'Edit', edit_yuuzi_path(@yuuzi) %> | <%= link_to 'Back', yuuzis_path %> これでいいんですよね。
8zca

2019/10/21 13:00

@yuuzi.avatar_path.url はあくまで画像のURLしか返しませんので、画像として表示させるためにimage_tagは変わらず必要です。 <%= image_tag @yuuzi.avatar_path.url %> で画像が出るはずです。
yamada_yuuki

2019/10/22 00:10

回答ありがとうございます。うまく表示できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問