Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法`全名';零级:零级_Ruby On Rails_Ruby_Methods_Status - Fatal编程技术网

Ruby on rails 未定义的方法`全名';零级:零级

Ruby on rails 未定义的方法`全名';零级:零级,ruby-on-rails,ruby,methods,status,Ruby On Rails,Ruby,Methods,Status,这是teamtreehouse.com上的一个练习 user.rb class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable,

这是teamtreehouse.com上的一个练习

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :profile_name

  validates :first_name, presence: true

  validates :last_name, presence: true

  validates :profile_name, presence: true,
                           uniqueness: true

  has_many :statuses

  def full_name
    first_name + " " + last_name
  end
end
显示第9行出现的/app/views/statuses/index.html.erb:

6: 
7: <% @statuses.each do |status| %>
8: <div class="status">
9:  <strong><%= status.user.full_name %></strong>
10:     <p><%= status.content %></p>
11:     <div class="meta">
12:       <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
6:
7: 
8: 
9:
10:

11: 12:

我真的很想知道为什么。

你好,我想你写错了

 User.rb file in model

has_many :statuses
请做适当的联想

很有可能

<% statuses.user.full_name %> 

在模型中添加属性
全名

attr_accessible :full_name
然后更新

 def full_name
    first_name + " " + last_name
  end
如下:

  def full_name
    self.full_name = self.first_name + " " + self.last_name
  end

问题是,当我在用户部件之前构建状态部分时,我生成了没有名字或姓氏的测试状态。


修复它的方法是打开rails控制台类型:
rails控制台
,然后是
Status.delete\u all

错误是因为某些记录没有一个名称,您可以使用以下代码避免错误

<%= status.user.try(:full_name) %>


如果全名起作用,它会以另一种方式将字符串放在“”或nil中

我想你是用复数写的,ruby可以理解吗???也尝试过了,只是为了检查一下,它实际上返回了一个将其更改为状态的错误。它不应该是
attr\u accessor
而不是
attr\u accessible
感谢您花时间发布此答案!救生员!
  def full_name
    self.full_name = self.first_name + " " + self.last_name
  end
<%= status.user.try(:full_name) %>