Ruby on Rails,查看属性

时间:2015-10-12 03:32:40

标签: ruby-on-rails ruby view

我得到了这个错误,对于我的生活,我无法弄清楚为什么。帮助将不胜感激。 :

错误3:error displayed after changes

错误4:after User.all.each do |user|

错误:未定义的方法'each'代表nil:nilClass

我的ruby / haml代码如下

查看器代码:

-#  This file is app/views/projects/index.html.haml
%h1 All Project Tasks

= form_tag projects_path, :method => :get do
  Include: 
  - @all_users.each do |user|
    = user
    = check_box_tag "users[#{user}]", 1, ("checked" if @filtered_users.find_index(user))
  = submit_tag 'Refresh', :id => "users_submit"


%table#projects
  %thead
    %tr
      %th{:class => ("hilite" if params[:sort] == "title")}= link_to "Title", {:controller => "projects", :sort => "title", :filter => @filtered_users.to_s}, :id => "title_header"
      %th Task Details
      %th Assigned Usertimot
      %th{:class => ("hilite" if params[:sort] == "due_date")}= link_to "Due Date", {:controller => "projects", :sort => "due_date", :filter => @filtered_users.to_s}, :id => "due_date_header"
  %tbody
    - @projects.each do |project|
      %tr
        %td= project.title 
        %td= link_to "More about #{project.title}", project_path(project)
        %td= project.user
        %td= project.due_date.to_formatted_s(:long)
 = link_to 'Add new project task', new_project_path

控制器代码:

class ProjectsController < ApplicationController

  def show
    id = params[:id] # retrieve project task ID from URI route
    @project = Project.find(id) # look up project task by unique ID
    # will render app/views/projects/show.<extension> by default

  def index
 @projects_users = Project.all_users  


# remembered settings
    if (params[:filter] == nil and params[:users] == nil and params[:sort] == nil and 
            (session[:filter] != nil or session[:users] != nil or session[:sort] != nil))
     if (params[:filter] == nil and session[:filter] != nil)
      params[:filter] = session[:filter]
     end
     if (params[:sort] == nil and session[:sort] != nil)
       params[:sort] = session[:sort]
      end
      redirect_to projects_path(:filter => params[:filter], :sort => params[:sort], :users => params[:users]) 
 else

      if (params[:filter] != nil and params[:filter] != "[]")
        @filtered_users = params[:filter].scan(/[\w-]+/)
       session[:filter] = params[:filter]
      else
       @filtered_users = params[:users] ? params[:users].keys : []
      session[:filter] = params[:users] ? params[:users].keys.to_s : nil
     end  
 end

 session[:sort] = params[:sort]
    session[:users] = params[:users] 


    if (params[:sort] == "title")
      if ( params[:users]or params[:filter] )
        @projects = Project.find(:all, :order => "title")
      end
 end
 if (params[:sort] == "due_date")
      if ( params[:users]or params[:filter] )
       @projects = Project.find(:all, :order => "due_date")
     end
    if (params[:sort] == nill)
    if(params[:users] or params[:filter])
     @projects = Project.all
    end
 end
end
end

 def new
  # default: render 'new' template
end

def create
    @project = Project.create!(project_params)
    flash[:notice] = "#{@project.title} was successfully created."
    redirect_to projects_path
 end

def edit
    @project = Project.find params[:id]
end

 def update
 @project = Project.find params[:id]
 @project.update_attributes!(project_params)
 flash[:notice] = "#{@project.title} was successfully updated."
 redirect_to project_path(@project)
end

 def destroy
  @project = Project.find(params[:id])
  @project.destroy
  flash[:notice] = "Project '#{@project.title}' deleted."
  redirect_to projects_path
 end

  private
   def project_params
     params.require(:project).permit(:title, :description, :extended_description, :user, :due_date)
    end
end
end

据我所知,haml的间距可能有点偏差,只是提前感谢格式化代码块的性质!

查看器代码:

class Project < ActiveRecord::Base
def self.all_users
   allUsers = []
   Project.all.each do |project|
     if (allUsers.find_index(project.user) == nil)
       allUsers.push(project.user)
     end
    end
    return allUsers
  end
end

2 个答案:

答案 0 :(得分:1)

您可能在视图中收到此行的错误:

@all_users.each do |user|

我发现错误的原因是您没有@all_users在控制器的index操作方法中实例化@all_users

首先将@projects_users切换为all_users。此外,project.rb中的nil方法似乎过于复杂,并且正在返回project.rb。尝试将class Project < ActiveRecord::Base def self.all_users all.includes(:user).map(&:user).uniq end end 修改为以下内容:

{{1}}

答案 1 :(得分:0)

  

未定义的方法&#39;每个&#39;为nil:nilClass

此错误基本上意味着您的变量中没有任何数据

在其他语言中,这意味着您 delcared 变量。因为Ruby是面向对象的,所以它将使用nilClass类填充变量。

许多新的Ruby开发人员被&#34;未定义的方法&#34;异常消息;它是你必须注意的nilClass

-

要正确解释错误,因为Ruby is object orientated,每个变量实际上都是数据对象,由表示。在Rails中,您可以将这些类定义为模型(User.find等)。

与其他语言不同,Ruby会按原样处理这些对象 - 它对它们使用方法。其他语言将数据拟合到函数中,E.G PHP&#39; each函数:

#PHP
<$ each($people) $>

#Ruby
<% @people.each do |person| %>

因此,&#34;没有方法&#34;错误基本上意味着Ruby无法找到您在nilClass上调用的方法。它抛弃了开发人员,因为他们认为&#34;我在x类&#34; 上有User方法,没有意识到变量已被{填充{1}}而不是。

缺点是你必须使你的电话有条件,或者正确填充变量。

错误似乎在这里:

nilClass
  
      
  1. 您根本没有分配#app/views/project/index.html.haml @all_users.each do |user| #app/controllers/projects_controller.rb class ProjectsController < ApplicationController def index @projects_users = Project.all_users end end
  2.   
  3. 您使用效率低下的方式来获取所有用户&#34;
  4.   

这就是我要做的事情:

@all_users

我对纯SQL缺乏经验,为了更清晰的观点,你最好提及joins documentation