shunta9922
@shunta9922 (shimo shun)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

NoMethodError

解決したいこと

Ruby on RailsでtwitterのようなWebアプリをつくっています。
logout機能を実装したらNoMethodError がでました。
おそらく部分テンプレートを導入したことが原因だと推測し、renderのlocalsに変数を記述してみたのですが依然エラーが解決しないのです
解決策またはヒントを頂けると幸いです。

発生している問題・エラー

NoMethodError in Users#show
Showing C:/app/views/users/_show.html.erb where line #2 raised:

undefined method `image_name' for nil:NilClass
Extracted source (around line #2):

<div class="user">
 <img src="<%= "/user_images/#{user.image_name}" %>">
 <h2><%= user.name %></h2>
 <p><%= user.email %></p>
  <div class="follow-button">
   <% unless current_user == user %>

Trace of template inclusion: app/views/users/show.html.erb

Rails.root: C:/Users/shunt/laforet_film_club/laforet_app

Application Trace | Framework Trace | Full Trace
app/views/users/_show.html.erb:2:in `_app_views_users__show_html_erb__1007084898_20600'
app/views/users/show.html.erb:4:in `_app_views_users_show_html_erb__651898738_20580'
Request
Parameters:

{"id"=>"logout"}

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

users/_show.html.erb
<div class="user">
 <img src="<%= "/user_images/#{user.image_name}" %>">
 <h2><%= user.name %></h2>
 <p><%= user.email %></p>
  <div class="follow-button">
   <% unless current_user == user %>
     <% if current_user.following?(user) %>
       <%= form_with(model:current_user.relationships.find_by(follow_id: user.id),html: { method: :delete }) do |f| %>
         <%= f.hidden_field :follow_id,value:user.id %>
         <%= f.submit 'フォローしない', class: 'btn btn-danger btn-block' %>     
       <% end %>
     <% else %>
        <%= form_with model:[current_user.relationships.build],local:true do |f| %>
         <%= f.hidden_field :follow_id,value:user.id %>
         <%= f.submit 'フォローする', class: 'btn btn-primary btn-block' %>
        <% end %>
     <% end %>
   <% end %>
  </div>
  <li>フォロー <%= followings_count %></li>
  <li>フォロワー <%= follower_count %></li>
 <% if user.id == current_user.id || current_user.admin? %>
     <%= link_to("編集",edit_user_path(user)) %>
     <%= link_to("削除",user_path(user)) %>
 <% end %>
</div>
_____________________________________________________________________________
users/show.html.erb
div class="main user-show">
  <div class="container">

   <%= render partial: 'show', locals:{user:@user,current_user:@current_user,followings_count:@followings_count,follower_count:@follower_count }%>

    <ul class="user-tabs">
      <li class="active"><%= link_to("投稿", user_path(@user)) %></li>
      <li><%= link_to("いいね!", likes_user_path(@user)) %></li>
      <li><%= link_to("フォロー",follow_user_path(@user)) %></li>
      <li><%= link_to("フォロワー",follower_user_path(@user)) %></li>
    </ul>
_______________________________________________________________________
users_controller
 before_action :follow_follower,{only:[:show,:follow,:follower,:likes]}

  def show  
   if @user.present?
     @followings_count =Relationship.where(user_id: @user.id).count
     @follower_count = Relationship.where(follow_id: @user.id).count
   end
  end
________________________________________________________________
User.rb
class User < ApplicationRecord
    has_secure_password

    validates :name,{presence: true}
    validates :email,{presence: true,uniqueness: true}

    def movie
        return Movie.where(user_id: self.id)
    end

    def follow_user
      return Relationship.where(user_id: self.id)
    end

    has_many :comment
    has_many :movie
    has_many :relationships
    has_many :followings, through: :relationships, source: :follow
    has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id'
    has_many :followers, through: :reverse_of_relationships, source: :user

 def follow(other_user)
    unless self == other_user
      self.relationships.find_or_create_by(follow_id: other_user.id)
    end
 end

  def unfollow(other_user)
    relationship = self.relationships.find_by(follow_id: other_user.id)
    relationship.destroy if relationship
  end

  def following?(other_user)
    self.followings.include?(other_user)
  end
end
__________________________________________________________________________________
schema

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "image_name"
    t.string "password_digest"
    t.boolean "admin", default: false
  end
_____________________________________________________________________________
routes
 logout_users POST   /users/logout(.:format)                                                                  users#logout 

0

2Answer

それぞれの言語に合わせてコードは分割して書いたほうが見やすい

def show
  if @user.present?
    @followings_count = Relationship.where(user_id: @user.id).count
    @follower_count = Relationship.where(follow_id: @user.id).count
  end
end

多分この時点で@userがない、nil。

どこでどの変数がnilか、最初から追っていくといいかも。

1Like

Comments

  1. @shunta9922

    Questioner

    アドバイスありがとうございます
    @userの抜けとルーティングに問題がありました
    無事解決しました

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌