【问题标题】:nomethoderror rails layoutnomethoderror 导轨布局
【发布时间】:2012-12-27 16:33:58
【问题描述】:

我是 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

【问题讨论】:

  • 请显示确切的错误文本以及状态控制器中索引方法的控制器代码。
  • 我已经更新了状态控制器和错误文本。谢谢@MichaelDurrant

标签: ruby-on-rails undefined nomethoderror


【解决方案1】:

在视图中您调用current_user.full_name,但current_usernil,并且无法在其上调用方法。 您可以执行以下操作:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2014-07-25
    • 1970-01-01
    相关资源
    最近更新 更多