当我尝试在rails应用程序上运行ruby时出现以下错误:
NoMethodError in Statuses#index
undefined method `full_name' for #<Status:0x3fe0e38>
我不明白为什么会收到这个错误,如果有人能指出我出错的地方,我将不胜感激!
Index.html.erb
<div class="page-header">
<h1>All Statuses</h1>
</div>
<%= link_to "Post A New Status", new_status_path, class: "btn btn-success" %>
<% @statuses.each do |status| %>
<div class="status">
<strong><%= status.full_name %></strong>
<p><%= status.content %></p></div>
<div class="meta">
<%= link_to time_ago_in_words(status.created_at) + " ago", status %>
<span class="admin">
| <%= link_to "Edit", edit_status_path(status) %> |
<%= link_to "Delete", status, method: :delete, data: { confirm: "Are you sure your want to delete this status?"} %>
</span>
</div>
</div>
<% end %>
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
# attr_accessible :title, :body
has_many :statuses
def full_name
first_name + " " + last_name
end
end
statuses_controller.rb
class StatusesController < ApplicationController
# GET /statuses
# GET /statuses.json
def index
@statuses = Status.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @statuses }
end
end
# GET /statuses/1
# GET /statuses/1.json
def show
@status = Status.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @status }
end
end
# GET /statuses/new
# GET /statuses/new.json
def new
@status = Status.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @status }
end
end
# GET /statuses/1/edit
def edit
@status = Status.find(params[:id])
end
# POST /statuses
# POST /statuses.json
def create
@status = Status.new(params[:status])
respond_to do |format|
if @status.save
format.html { redirect_to @status, notice: 'Status was successfully created.' }
format.json { render json: @status, status: :created, location: @status }
else
format.html { render action: "new" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# PUT /statuses/1
# PUT /statuses/1.json
def update
@status = Status.find(params[:id])
respond_to do |format|
if @status.update_attributes(params[:status])
format.html { redirect_to @status, notice: 'Status was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @status.errors, status: :unprocessable_entity }
end
end
end
# DELETE /statuses/1
# DELETE /statuses/1.json
def destroy
@status = Status.find(params[:id])
@status.destroy
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
end
答案 0 :(得分:0)
因为Status
没有full_name
。
您的User
模型确实如此,但您似乎没有尝试显示用户,而是显示状态。每个用户看起来都有独特的状态,因此您只需打印状态的用户的全名即可。
那就是说,除非你真的需要每个用户的任意状态,否则通常你可能有一个状态表,以及相关用户的状态和连接表,这会使这种技术不合适。
答案 1 :(得分:0)
您正在状态对象上调用full_name,但该方法是在用户模型中定义的。如果您希望用户的全名迭代状态,您可以执行以下操作:
<%= status.user.full_name %>
假设状态中的关联存在于用户
答案 2 :(得分:0)
实际上,您应该在full_name
对象而不是User
对象上调用Status
。
<strong><%= status.user.full_name %></strong>
假设它们都是关联的(使用belongs_to :user
),这应该有效。