0

我是 Rails 新手,并试图在我的标题中创建一个链接以登录/退出个人资料。我不是以下代码的问题,特别是粗体(**)区域:

<body>
<div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <a href="#" class="brand"> Test App</a>
            <ul class="nav">
        <li><%= link_to "All Statuses", statuses_path %></li>
      </ul>
      <ul class="nav pull-right">
        **<li><%= link_to current_user.full_name, "#" %></li>**

      </ul>
        </div>
    </div>
</div>
<div class="container">
<p class="notice"><%= notice %></p>
   <p class="alert"><%= alert %></p>
    <%= yield %>
</div>

我不断收到一条错误消息:

状态中的 NoMethodError#index

显示 /Users/test_app/app/views/layouts/application.html.erb 其中第 18 行提出:

nil:NilClass 的未定义方法 `full_name' 提取的源代码(第 18 行附近):

 <li><%= link_to "All Statuses", statuses_path %></li>
      </ul>
      <ul class="nav pull-right">
        <li><%= link_to current_user.full_name, "#" %></li>

      </ul>
    </div>

状态控制器代码是:

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
4

1 回答 1

1

在你调用的视图current_user.full_name中,但是current_userisnil和method 不能在它上面调用。您可以执行以下操作:

<ul class="nav pull-right">
  <% if current_user %>
    <li><%= link_to current_user.full_name, "#" %></li>
  <% end %>
</ul>

这将仅在用户登录时显示您的链接(这意味着current_user将返回用户实例,而不是nil

于 2012-12-27T16:53:18.513 回答