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

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

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

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Ruby on Rails

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

Q&A

解決済

1回答

1586閲覧

エラーメッセージ:undefined method `image?' for #<Article::ActiveRecord_Relation:0x00007f1ced8c6908>が解決できない

sss-kk

総合スコア3

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/06/23 04:29

閲覧ありがとうございます。
現在Ruby on Railsにてtwitter風アプリのようなポートフォリオを作成中です。
投稿一覧にて画像の表示をさせたいです。
いろいろな方々の記事を参考にして、Articles#indexにmethodがないことはわかったのですが
どこがどうしたらよいのかわかりません。

画像連携はS3を使用しています。エディタはcloud9を使用しています
記事の編集画面(article#edit)では画像の確認ができるので連携は問題なさそうです。
article#Indexかviewが間違っているのではないかと考えます。

途中から参考記事と他の記事が頭の中で混ざってしまい、他のミスも増えているかもしれません。
拙いコードで大変申し訳ございませんがご教授いただければ幸いです。

関係のあるコード

エラーメッセージ

ActionView::Template::Error (undefined method `image?' for #<Article::ActiveRecord_Relation:0x00007f1ced8c6908>): 4: <!--lメソッドを使って日時表示の設定+formatオプションの指定--> 5: <%= l article.created_at, format: :short %> 6: <!-- 画像表示? --> 7: <% if @article.image? %> 8: <%= image_tag @user.image.url %> 9: <% end %> 10: app/views/articles/_article.html.erb:7:in `_app_views_articles__article_html_erb__4002249967374245585_69881111526080' app/views/articles/index.html.erb:6:in `_app_views_articles_index_html_erb__2065459800753556665_69881110614340'

views/articles/edit.html.erb

articles/edit.html.erb

1<h1>Editing Article</h1> 2 3<%= render 'form', article: @article %> 4 5<%= link_to 'Back', articles_path %>

views/articles/index.html.erb

articles/index.html.erb

1<h1>Articles</h1> 2<%= link_to 'New Article', new_article_path %> 3<!--render partial: 'articles/article.html.erb', locals: { article: @articles }のこと--> 4<!--articlesフォルダのパーシャル _article.html.erb を呼び出してね--> 5<!--その画面のローカル変数 articleには インスタンス変数(オブジェクト)の @articlesを渡してね--> 6<%= render @articles %> 7 8 9<%= paginate @articles %> 10

views/articles/_article.html.erb

_article.html.erb

1<div class="card mb-3"> 2 <div class="card-body"> 3 <h5 class="card-title"><%= article.title %></h5> 4 <!--lメソッドを使って日時表示の設定+formatオプションの指定--> 5 <%= l article.created_at, format: :short %> 6 <!-- 画像表示? --> 7 <% if @article.image? %> 8 <%= image_tag @user.image.url %> 9 <% end %> 10 11 <!--ログインしたユーザーIDと投稿したユーザーIDが同じなら--> 12 <% if user_signed_in? && current_user.id == article.user_id %> 13 <%= link_to article.user.name,users_myprofile_path %> 14 <%= link_to 'Edit', edit_article_path(article), class: "btn btn-outline-primary" %> 15 <%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-outline-danger" %> 16 17 <% else %> 18 <!--投稿者の表示 --> 19 <%= link_to(article.user.name,"/users/#{article.user.id}") %> 20 <% end %> 21 </div> 22</div>

articles_controller.rb

articles_controller.rb

1class ArticlesController < ApplicationController 2 #ログインしてなかったらはじく 3 before_action :authenticate_user! 4 5 before_action :set_article, only: [:show, :edit, :update, :destroy] 6 7 # GET /articles 8 def index 9 # publicの記事のみ表示 10 # ページネーションをつけたいデータに.page(params[:page])を追加 11 @articles = Article.where("status = 0").page(params[:page]).per(15).order(created_at: :desc) 12 13 @article = Article.all 14 end 15 16 # GET /articles/1 17 def show 18 #非公開記事をログインユーザー以外がアクセスした場合の処理 19 if @article.status_private? && @article.user != current_user 20 respond_to do |format| 21 format.html { redirect_to articles_path, notice: 'このページにはアクセスできません' } 22 end 23 end 24 end 25 26 # GET /articles/new 27 def new 28 @article = Article.new 29 @articles = Article.find(params[:id]) 30 end 31 32 # GET /articles/1/edit 33 def edit 34 @article = Article.find(params[:id]) 35 end 36 37 # POST /articles 38 def create 39 #ユーザーとの関係性を指定する 40 @article = current_user.articles.new(article_params) 41 42 if @article.save 43 redirect_to @article, notice: 'Article was successfully created.' 44 else 45 format.html { render :new } 46 format.json { render json: @article.errors, status: :unprocessable_entity } 47 render :new 48 end 49 end 50 51 # PATCH/PUT /articles/1 52 def update 53 #set_article参照 54 if @article.update(article_params) 55 redirect_to @article, notice: 'Article was successfully updated.' 56 else 57 render :edit 58 end 59 end 60 61 # DELETE /articles/1 62 def destroy 63 #set_article参照 64 @article.destroy 65 redirect_to articles_url, notice: 'Article was successfully destroyed.' 66 end 67 68 def otherIndex 69 # publicの記事のみ表示 70 # ページネーションをつけたいデータに.page(params[:page])を追加 71 @articles = Article.where(user_id: params[:id]).where("status = 0").page(params[:page]).per(15).order(created_at: :desc) 72 @user = User.find(params[:id]) 73 end 74 75 private 76 # Use callbacks to share common setup or constraints between actions. 77 def set_article 78 #自分の記事 79 @article = current_user.articles.find_by(id: params[:id]) 80 end 81 82 # Only allow a trusted parameter "white list" through. 83 def article_params 84 #:user_idを追加 85 #permit→保存したいカラムを指示 86 params.require(:article).permit( 87 :title, 88 :user_id, 89 :image, 90 :status, {:cat_ids => []} 91 ) 92 end 93end

他必要なコードがございましたらご指示ください。
よろしくお願いいたします

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーメッセージのとおりです。

@article = Article.allと、コレクションを代入していますが、このコレクションにimage?メソッドがありません。

投稿2021/06/23 04:57

maisumakun

総合スコア145183

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

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

sss-kk

2021/06/23 11:55

maisumakun様 ご回答ありがとうございます。 Article.allの件ですが、 schema.rbの"article"テーブルには create_table "articles", force: :cascade do |t| t.integer "user_id" t.string "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "status", default: 0, null: false t.string "image" t.index ["user_id"], name: "index_articles_on_user_id" end とimageは入っているようですがこちらのことですか? "edit"編集画面ではうまくimageが表示できていたのでDBとの連携はうまくいっていて、やはりarticles_controller.rbかviews/articles/_article.html.erbのコードが間違っているということでしょうか? 質問ばかりで申し訳ございません、よろしくお願いします
maisumakun

2021/06/23 12:05

> こちらのことですか? 回答内で「コレクション」と書いたように、複数のレコードを持った状態になっているので、どれに対してimage?なのかわからない、という話です。
sss-kk

2021/06/28 06:14

ありがとうございます。 <% if @article.image? %> <%= image_tag @user.image.url %> ↓ <% if article.image? %> <%= image_tag article.image.url %> へ訂正したところ無事一覧を表示することができました! 質問したコードでは@articleで別に変数を定義していましたが、 index.html.erb内でrenderを使用していたので _article.html.erb内ではarticle: @articles(インスタンス変数)でした。 別に定義する必要はなく、この@articlesを使用したコードの書き方をするべきでした。 とても助かりました、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問