第一个问题,请温柔:)
我在为属于具有 has_many 关联的用户模型的客户端模型创建索引视图时遇到问题。
错误信息:
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
具体来说,错误是指第 11 行的部分错误:
/views/clients/index.html
<% provide(:title, current_user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>Your clients</h1>
    </section>
  </aside>
  <div class="span8">
    <% if current_user.clients.any? %>
      <ol class="clients">
        <%= render @clients %>
      </ol>
      <%= will_paginate %>
    <% end %>
  </div>
</div>
/clients/_client.html.erb
<li>
  <span class="client-name"><%= client.name %></span>
  <span class="client-info">
    Extra client info to come.
  </span>
</li>
客户端控制器:
class ClientsController < ApplicationController
  belongs_to :user
  def index
    @clients = current_user.clients.paginate(page: params[:page])
  end
编辑:
用户控制器是否有帮助...
class UsersController < ApplicationController
  before_filter :authenticate_user!
  def show
    if current_user.admin?
        @user = User.find(params[:id])
    else
        @user = current_user
    end
  end
  def index
    if current_user.admin?
        @users = User.paginate(page: params[:page])
    else
        redirect_to root_path
    end
  end
  def destroy
        User.find(params[:id]).destroy
        flash[:success] = "User destroyed."
        redirect_to users_path
    end
  end
正如您可能会告诉我的那样,我是 Rails 的新手,但已经搜索以确保这还没有被覆盖。