Ruby初心者です。
簡単なポートフォリオの作成を行なっております。
現在、以下のようなエラーにハマってしまい、沢山調べたのですが解決することが出来なく困っています。
そこで皆様のお力を借りたいと思い、この度、質問させて頂きます。
エラー内容は、
NoMethodError in Posts#index undefined method `image'
というものです。
エラーが出ているのは、こちらの箇所です。
posts/index.html.erb
<%= image_tag post.photos.first.image.url(:medium) , class: "card-img-top"%>
↓参考箇所↓
posts_controller.rb
def index @posts = Post.limit(10).includes(:photos, :user) end
routes
Rails.application.routes.draw do devise_for :users root to: 'pages#home' resources :users, only: %i(show index) resources :posts, only: %i(index new create show) do resources :photos, only: %i(create) end end
posts/index.html.erb
<% @posts.each do |post| %> <div class="col-md-8 col-md-2 mx-auto"> <div class="card-wrap"> <div class="card"> <div class="card-header align-items-center"> <%= link_to user_path(post.user), class: "no-text-decoration" do %> <%= image_tag avatar_url(post.user), class: "post-profile-icon" %> <% end %> <%= link_to user_path(post.user), class: "black-color no-text-decoration",title: post.user.name do %> <strong><%= post.user.name %></strong> <% end %> </div> <%= image_tag post.photos.first.image.url(:medium) , class: "card-img-top"%> <div class="card-body"> <div class="row parts"> <%= link_to "", "#", class: "love" %> <%= link_to "", "#", class: "comment" %> </div> <div><strong>「いいね!」10件</strong></div> <div> <span><strong><%= post.user.name %></strong></span> <span><%= post.caption %></span> <%= link_to time_ago_in_words(post.created_at).upcase + "前", post_path(post), class: "post-time no-text-decoration" %> <hr> <div class="row parts"> <form action="#" class="w-100"> <div> <textarea class="form-control comment-input border-0" placeholder="コメント..." rows="1"></textarea> </div> </form> </div> </div> </div> </div> </div> </div> <% end %>
回答依頼をいただいておいて申し訳ないのですが、Rubyについての知識はほぼ皆無なのでもう少し知見をもった方に回していただければと。
回答1件
あなたの回答
tips
プレビュー