NoMethodError(未定义的方法projects' for nil:NilClass):
app/controllers/project_controller.rb:8:in
索引'
Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (21.7ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.2ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.6ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (89.7ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.6ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.6ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.7ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (51.7ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (1.2ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.9ms) Rendered /Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (116.8ms)
这是一个项目控制器:
class ProjectController < ApplicationController
before_action :confirm_logged_in
before_action :find_company
def index
@projects = @company.projects.sorted
end
def show
@project = Project.find(params[:id])
end
def new
@project = Project.new()
@project_count = Project.count + 1
@companys = Company.order("position ASC")
end
def create
@project = Project.new(project_params)
if @project.save
redirect_to(:action => 'index')
else
@project_count = Project.count + 1
@companys = Company.order("position ASC")
render('new')
end
end
def edit
@project = Project.find(params[:id])
@project_count = Project.count
@companys = Company.order("position ASC")
end
def update
@project = Project.find(params[:id])
if @project.update_attributes(project_params)
redirect_to(:action => 'index')
else
@project_count = Project.count
@companys = Company.order("position ASC")
render('new')
end
end
def delete
@project = Project.find(params[:id])
end
def destory
@project = Project.find(params[:id])
@project.destroy
redirect_to(:action => 'index')
end
private
def project_params
params.require(:project).permit(:name, :position, :type_of_project, :description, :no_of_tasks)
end
def find_company
if params[:company_id]
@company = Company.find(params[:company_id])
end
end
end
答案 0 :(得分:0)
错误表示@company
为nil
。公司来自方法find_company
,您可以更正find_company
逻辑,也可以在请求中提供company_id
参数。如果index
为零,您还可以调整@company
方法以获取所有项目:
def index
@projects = Project.all.sorted
end
这完全取决于您希望如何加载项目。
答案 1 :(得分:0)
您的链接网址或按钮网址是指索引页面的内容。您需要通过:company_id
参数才能使其正常工作。
e.g。
projects_path(:company_id => Company.first.id)