“nil"不是返回有效部分路径的 ActiveModel 兼容对象 [英] 'nil' is not an ActiveModel-compatible object that returns a valid partial path

查看:44
本文介绍了“nil"不是返回有效部分路径的 ActiveModel 兼容对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题,请温柔:)

我无法为属于具有 has_many 关联的用户模型的客户端模型创建索引视图.

错误信息:

'nil' 不是返回有效部分路径的 ActiveModel 兼容对象.

特别是错误是指第 11 行的部分错误:

/views/clients/index.html

<% provide(:title, current_user.name) %><div class="row"><aside class="span4"><部分><h1>您的客户</h1></节></一边><div class="span8"><% if current_user.clients.any?%><ol class="clients"><%= 渲染@clients %></ol><%= will_paginate %><%结束%>

/clients/_client.html.erb

  • <span class="client-name"><%= client.name %></span><span class="client-info">额外的客户信息即将到来.</span>
  • 客户端控制器:

    class ClientsController <应用控制器归属地:用户定义索引@clients = current_user.clients.paginate(page: params[:page])结尾

    用户控制器,如果有帮助...

    class UsersController <应用控制器before_filter :authenticate_user!高清秀如果 current_user.admin?@user = User.find(params[:id])别的@user = current_user结尾结尾定义索引如果 current_user.admin?@users = User.paginate(page: params[:page])别的重定向到 root_path结尾结尾销毁User.find(params[:id]).destroyflash[:success] = "用户已销毁."重定向到用户路径结尾结尾

    你可能会说我是 Rails 的新手,但我已经搜索过以确保这还没有被覆盖.

    解决方案

    我在客户端控制器和模型中声明了belongs_to,而不仅仅是模型.而且两天都没注意到.

    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.

    解决方案

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

    这篇关于“nil"不是返回有效部分路径的 ActiveModel 兼容对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    其他开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆