问题
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