'nil' is not an ActiveModel-compatible object that returns a valid partial path

两盒软妹~` 提交于 2019-12-25 05:25:54

问题


First question, please be gentle :)

I am having trouble creating an index view for a client model that belongs_to the user model with a has_many association.

The error message:

'nil' is not an ActiveModel-compatible object that returns a valid partial path.

Specifically the error refers to the partial on line #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>

Clients controller:

class ClientsController < ApplicationController
  belongs_to :user

  def index
    @clients = current_user.clients.paginate(page: params[:page])
  end

EDIT:

Users controller if it helps...

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

As you can probably tell I am new to rails, but have searched to ensure this hasn't been covered already.


回答1:


Should you be passing :page => params[:page] to paginate instead?




回答2:


I declared belongs_to in the clients controller and the model instead of just the model. And didn't notice for two days.



来源:https://stackoverflow.com/questions/11968396/nil-is-not-an-activemodel-compatible-object-that-returns-a-valid-partial-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!