Я пытаюсь проверить метод index моего track_controller, и он продолжает возвращать <500 Internal Server Error>
.
Трек, созданный пользователем, связан с указанным пользователем.Поэтому @track.user.name
должно вернуть имя пользователя.Однако ошибка указывает на то, что user
возвращает nil
, несмотря на то, что я явно назначаю пользователя, что дает?
Журнал испытаний:
-------------------------------------------
TracksControllerTest: test_should_get_index
-------------------------------------------
[1m[36mUser Load (0.3ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 701806347], ["LIMIT", 1]]
[1m[36mTrack Load (0.1ms)[0m [1m[34mSELECT "tracks".* FROM "tracks" WHERE "tracks"."id" = ? LIMIT ?[0m [["id", 980190962], ["LIMIT", 1]]
[1m[35m (0.1ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mTrack Update (0.3ms)[0m [1m[33mUPDATE "tracks" SET "user_id" = ?, "updated_at" = ? WHERE "tracks"."id" = ?[0m [["user_id", 701806347], ["updated_at", "2018-12-11 14:30:49.753966"], ["id", 980190962]]
[1m[35m (0.1ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
Started GET "/" for 127.0.0.1 at 2018-12-11 14:30:49 +0000
Processing by TracksController#index as HTML
[1m[35m (0.1ms)[0m [1m[35mSAVEPOINT active_record_1[0m
[1m[36mCart Create (0.2ms)[0m [1m[32mINSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)[0m [["created_at", "2018-12-11 14:30:49.856142"], ["updated_at", "2018-12-11 14:30:49.856142"]]
[1m[35m (0.0ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m
Rendering tracks/index.html.haml within layouts/application
[1m[36mTrack Load (0.2ms)[0m [1m[34mSELECT "tracks".* FROM "tracks" ORDER BY created_at desc[0m
Rendered tracks/index.html.haml within layouts/application (23.1ms)
Completed 500 Internal Server Error in 51ms (ActiveRecord: 0.5ms)
NoMethodError - undefined method `name' for nil:NilClass:
app/views/tracks/index.html.haml:20:in `block in _app_views_tracks_index_html_haml___68907435507065187_70307785248520'
app/views/tracks/index.html.haml:8:in `_app_views_tracks_index_html_haml___68907435507065187_70307785248520'
test/controllers/tracks_controller_test.rb:17:in `block in <class:TracksControllerTest>'
track_controller_test.rb:
require 'test_helper'
include Warden::Test::Helpers
class TracksControllerTest < ActionDispatch::IntegrationTest
setup do
@user = users(:cscades)
login_as(@user)
@track = tracks(:one) #need to sign in the users in order for tracks to be deleted as only track creators can delete them
@track.user = @user
@track.save!
end
test "should get index" do
get root_path # root path == tracks#index
assert_response :success
end
track_controller.rb:
class TracksController < ApplicationController
skip_before_action :authenticate_user!, raise: false
before_action :set_track, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /tracks
# GET /tracks.json
def index
@tracks = Track.all.order("created_at desc")
end
index.html.haml (для треков) ошибка появляется в строке, указанной ниже:
- content_for :header do
%section.hero.is-warning
.hero-body
.container
%h1.title
Browse the newest Tracks
.track-index-grid.pt4
- @tracks.each do |track|
.track.border-light
.track-thumb
-unless track.image_url(:thumb).nil?
=link_to image_tag(track.image_url(:thumb)), track
- if track.genre
.condition
%span.tag.is-dark
= track.genre
.pa3
%h3.fw7.f4.title= link_to track.name, track
%p.has-text-gray.fw7.pt9
Sold by #{link_to track.user.name, new_message_path(:recipient => track.user.email), :title => "Send an email to #{track.user.name}"} #ERROR OCCURS HERE
%p.has-text-grey
#{track.description}
%p.f3.fw6.has-text-right.pt2.price= number_to_currency(track.price)
- if track_artist(track)
= link_to 'Edit', edit_track_path(track), class: "button is-small"
= link_to 'Delete', track, method: :delete, data: { confirm: "Are you sure ?" }, class: "button is-small"
users.yml (пользовательские фиксации):
cscades:
name: Cscades
encrypted_password: <%= Devise::Encryptor.digest(User,'password') %>
email: cscadesbooking@gmail.com
# column: value
#
polocardigan:
name: PoloCardigan
encrypted_password: <%= Devise::Encryptor.digest(User,'password') %>
email: cardiganbooking@gmail.com
# column: value