Home > Enterprise >  How to Fix Template Error Undefined Method?
How to Fix Template Error Undefined Method?

Time:11-05

I'm creating a movie review website with Ruby on Rails. A test showed the error below after adding codes of showing movies' title on posts of reviews.

Error

ERROR["test_profile_display", UsersProfileTest, 5.1500919768586755]
test_profile_display#UsersProfileTest (5.15s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `title' for nil:NilClass
            app/views/comments/_comment.html.erb:5:in `_app_views_comments__comment_html_erb__857699480521752031_47037482885220'
            app/views/users/show.html.erb:19:in `_app_views_users_show_html_erb___3985789656075004903_47037471840320'
            test/integration/users_profile_test.rb:14:in `block in <class:UsersProfileTest>'

Codes Relating to the Error

  1. _comment.html.erb (partial)
<li id="comment-<%= comment.id %>">
  <%= link_to gravatar_for(comment.user, size: 50), comment.user %>
  <span ><%= link_to comment.user.name, comment.user %></span>
  <span ><%= comment.content %></span> 
  <span ><%= link_to comment.movie.title, "/movies/#{comment.movie_id}" %></span> #relevant part
  <span >
    Posted <%= time_ago_in_words(comment.updated_at) %> ago.
    <% if current_user?(comment.user) %>
      <%= link_to "edit", "/comments/#{comment.id}/edit?movie_id=#{comment.movie_id}" %>
      <%= link_to "delete", comment, method: :delete,
                                     data: { confirm: "You sure?" } %>
    <% end %>
  </span>
</li>
  1. show.html.erb
<% provide(:title, @user.name) %>
<div >
  <aside >
    <section >
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
    <section >
      <%= render 'shared/stats' %>
    </section>
  </aside>
  <div >
    <%= render 'follow_form' if logged_in? %>
    <% if @user.comments.any? %>
      <h3>Comments (<%= @user.comments.count %>)</h3>
      <ol >
        <%= render @comments %> #relevant part
      </ol>
      <%= will_paginate @comments %>
    <% end %>
  </div>
</div>
  1. users profile test.erb
equire 'test_helper'

class UsersProfileTest < ActionDispatch::IntegrationTest
  include ApplicationHelper

  def setup
    @user = users(:michael)
  end

  test "profile display" do
    get login_path
    post login_path, params: { session: { email: @user.email, 
                                          password: 'password' } }
    get user_path(@user) #relevant part
    assert_template 'users/show'
    assert_select 'title', full_title(@user.name)
    assert_select 'h1', text: @user.name
    assert_select 'h1>img.gravatar'
    assert_match @user.comments.count.to_s, response.body
    assert_select 'div.pagination'
    @user.comments.paginate(page: 1).each do |comment|
      assert_match comment.content, response.body
    end
  end
end
  1. comment.rb (model)
class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :movie, optional: true, primary_key: "id"
  has_many :favorites, dependent: :destroy
  
  
  default_scope -> { order(created_at: :desc) }
  validates :user_id,   presence: true
  validates :movie_id,  presence: true
  validates :content,   presence: true, length: { maximum: 250}
  
end

Actually, the system of showing titles works well, which means the test have some problems I think. The error said "undefined method" though, I don't know where and how I should define the method.

Any solutions?

Thanks.

CodePudding user response:

I rewrote codes of _comment.html.erb (partial) like the below.

<% if comment.movie %>
  <span ><%= link_to comment.movie.title, "/movies/#{comment.movie_id}" %></span>
<% end %>

Then, passed the test.

Thanks.

  • Related