是我的项目类是nill还是什么,谁能解释我这个错误。
NoMethodError (未定义方法
projects' for nil:NilClass):)app/controllers/project_controller.rb:8:in指数‘ 呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.2ms)呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.4ms)呈现/Users/ajaysithagari/..rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)在救援/布局(50.0ms)中呈现/用户/ajaysithagari/..rvm/gems/ruby-2中的/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb。2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.4ms)在布局中呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb /内联字符串(0.3ms)呈现/用户/ajaysithagari/..rvm/gems/ruby2.2.1/布局中的gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb /内联_string (0.3ms)在布局中呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb /内联_string (0.4ms)呈现/User/ajaysithagari/..rvm/gems/ruby2.2.1布局中的/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb /javascript (41.3ms)在布局中呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb /javascript (0.3ms)呈现/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.布局中的2.1/lib/web_console/templates/error_page.js.erb /javascript (0.4ms)呈现为/Users/ajaysithagari/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (93.4ms)
这是我的项目负责人
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()
end
def create
@project = Project.new(project_params)
if @project.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
@project = Project.find(params[:id])
end
def update
@project = Project.find(params[:id])
if @project.update_attributes(project_params)
redirect_to(:action => 'index')
else
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实际上,在进入项目之前,我们需要找到company_id,索引@projects = @company.projects.sorted意味着公司有很多项目。
发布于 2015-11-20 08:21:13
问题在于if params在def find_company中。对于index函数,没有设置它。
将def索引更改为:
def index
@projects = Project.sorted
end更新:犯了错误,我想:
def index
@projects = Project.all.sorted #or leave the call to sorted out completely
endhttps://stackoverflow.com/questions/33821853
复制相似问题