【发布时间】:2015-12-24 18:02:19
【问题描述】:
在我的index.html.erb 文件中,我得到了一个未定义的电子邮件方法。我进入 Rails 控制台并尝试运行 User.first.email,它运行良好。我尝试取出电子邮件并运行link.user,这也奏效了。我不确定当用户绑定到链接并且用户有电子邮件时,为什么电子邮件会被视为未定义。我正在寻找一些关于可能出现问题的建议。
Index.html.erb
<%= link.user.email %>
已运行迁移的架构
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
型号:
class Link < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
acts_as_votable
attr_accessor :avatar
mount_uploader :avatar, AvatarUploader
end
class User < ActiveRecord::Base
has_many :links
acts_as_voter
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
链接控制器:
class LinksController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@links = Link.all
end
def show
@link = Link.find(params[:id])
end
def new
@link = Link.new
end
def edit
end
def create
@link = Link.new(link_params)
if @link.save
redirect_to root_path
else
render 'new'
end
end
private
def link_params
params.require(:link).permit(:title, :url, :avatar, :user_id)
end
end
错误信息:
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendered links/_form.html.erb (2.7ms)
Rendered links/new.html.erb within layouts/application (3.8ms)
Completed 200 OK in 505ms (Views: 143.6ms | ActiveRecord: 0.4ms)
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2