在我的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)
答案 0 :(得分:5)
class Foo
constructor: ->
@list = []
您收到此错误是因为某些链接undefined method `email' for nil:NilClass link.user.email
为link.user
,因此会调用nil
并因上述错误消息而失败。
您可以使用try来解决此问题:
nil.email
这样,即使某些情况下<%= link.user.try(:email) %>
为link.user
,您的程序也不会崩溃。
答案 1 :(得分:1)
此错误的原因是link.user
是nil
。您必须检查它是否为nil
:
<% if !link.user.nil? %>
<%= link.user.email %>
<% end %>
答案 2 :(得分:1)
特定nil
可能会有email
nil
user
您可以使用try
逃离undefined method for NilClass
<%= link.try(:user).try(:email) %>
如果user
的{{1}}为零,则会保存您
或
link
答案 3 :(得分:1)
我知道你接受了另一个答案,但我认为这是不正确的。
错误表示您没有user
的关联link
对象。
如果您希望每个link
拥有关联的user
,那么您最好使用validation
确保&create
#39;出现在update
或#app/models/link.rb
class Link < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
end
:
link
这意味着每次创建User
时,都有拥有.nil?
个对象;首先防止错误。
-
如果您需要运行应用程序的方式(IE只有用户可以发布链接或其他内容),那么您将不再需要.try()
或public class TestActivity extends AppCompatActivity {
private Handler mHandler = new Handler();
//private long mStartRX = 0;
//private long mStartTX = 0;
private long mLastRX = 0;//Store Last RX data
private long mLastTX = 0;//Store Last TX data
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tv_remote);
mLastRX = TrafficStats.getTotalRxBytes(); //Set initial RX data
mLastTX = TrafficStats.getTotalTxBytes(); //Set initial TX data
if (mStartRX == TrafficStats.UNSUPPORTED
|| mStartTX == TrafficStats.UNSUPPORTED) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Uh Oh!");
alert.setMessage("Your device does not support traffic stat monitoring.");
alert.show();
} else {
mHandler.postDelayed(mRunnable, 1000);
}
//
}
private void calculate() {
long rxBytes = TrafficStats.getTotalRxBytes(); // Latest RX data
long rxSpeed = rxBytes - mLastRX; // Diff = Latest RX data - Last RX Data
Log.i("TAG", "Speed RX:"+rxSpeed);
mLastRX = rxBytes; //Set last RX data as Current RX data
RX.setText(Long.toString(rxSpeed) + " bytes");
if (rxSpeed >= 1024) {
// KB or more
long rxKb = rxSpeed / 1024;
RX.setText(Long.toString(rxKb) + " KBs");
if (rxKb >= 1024) {
// MB or more
long rxMB = rxKb / 1024;
RX.setText(Long.toString(rxMB) + " MBs");
if (rxMB >= 1024) {
// GB or more
long rxGB = rxMB / 1024;
RX.setText(Long.toString(rxGB));
}// rxMB>1024
}// rxKb > 1024
}// rxSpeed>=1024
long txBytes = TrafficStats.getTotalTxBytes(); // Latest TX data
long txSpeed = txBytes - mLastTX; // Diff = Latest TX data - Last TX Data
mLastTX = txBytes; //Set last TX data as Current TX data
Log.i("TAG", "Speed TX:"+txSpeed);
TX.setText(Long.toString(txSpeed) + " bytes");
if (txSpeed >= 1024) {
// KB or more
long txKb = txSpeed / 1024;
TX.setText(Long.toString(txKb) + " KBs");
if (txKb >= 1024) {
// MB or more
long txMB = txKb / 1024;
TX.setText(Long.toString(txMB) + " MBs");
if (txMB >= 1024) {
// GB or more
long txGB = txMB / 1024;
TX.setText(Long.toString(txGB));
}// rxMB>1024
}// rxKb > 1024
}// rxSpeed>=1024
}
}
条件语句图。
简而言之,我认为接受的答案就像画画一样生锈(没有冒犯等)。
答案 4 :(得分:0)
我认为您需要检查链接迁移文件
onChildUpdated
在此文件中,您需要class CreateLinks < ActiveRecord::Migration