首页 > 解决方案 > nil:Nilclass 错误的未定义方法电子邮件

问题描述

我通过设计有用户。这就是我的用户模型。

我有家庭控制器

class HomeController < ApplicationController
  before_action :authenticate_user!

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end
end

我的登录没问题。我的索引页面工作正常。

我的显示页面显示登录用户的推文......

<h1>Profile Page for <%= @user.email %></h1>
  <p><%= @user.email %></p>
  <% @user.tweets.each do |tweet| %>
<p><%= tweet.content %></p>
<% end %>

当我尝试去 localhost:3000/users/1

undefined method `email' for nil:NilClass (error on first line)

我的路线

Rails.application.routes.draw do
  get 'home/index'
  get 'home/show'
  devise_for :users
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root to: 'home#index'

  match 'users/:id' => 'home#show', via: :get


end

我该如何解决这个问题?

标签: ruby-on-railsruby-on-rails-5

解决方案


推荐阅读