Ruby on rails 使用生成创建中未定义的方法
我一直在反复讨论这个问题,不明白为什么Rails 4会出错 我有3个模型,如下所示 我正在尝试创建一个与项目关联的节,并为nil:NilClass获得未定义的方法'sections'的命名错误 据我所知,我正在经历与创建与当前用户关联的项目时相同的过程 非常感谢您的帮助 user.rbRuby on rails 使用生成创建中未定义的方法,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我一直在反复讨论这个问题,不明白为什么Rails 4会出错 我有3个模型,如下所示 我正在尝试创建一个与项目关联的节,并为nil:NilClass获得未定义的方法'sections'的命名错误 据我所知,我正在经历与创建与当前用户关联的项目时相同的过程 非常感谢您的帮助 user.rb class User < ActiveRecord::Base devise :invitable, :database_authenticatable, :registerable, :r
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :projects
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :project
end
Dr::Application.routes.draw do
resources :projects do
resources :sections
end
resources :sections
root :to => "home#index"
devise_for :users
end
def new
@section = Section.new
end
def create
@project = params[:project_id]
@section = @project.sections.build section_params
respond_to do |format|
if @section.save
format.html { redirect_to project_path(@section), :notice => 'Section was successfully created.' }
format.xml { render :xml => @section, :status => :created, :location => @section }
else
format.html { render :action => "new" }
format.xml { render :xml => @section.errors, :status => :unprocessable_entity }
end
end
end
private
def section_params
params.require(:section).permit(:name)
end
部分\u controller.rb
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :projects
end
class Project < ActiveRecord::Base
belongs_to :user
has_many :sections
end
class Section < ActiveRecord::Base
belongs_to :project
end
Dr::Application.routes.draw do
resources :projects do
resources :sections
end
resources :sections
root :to => "home#index"
devise_for :users
end
def new
@section = Section.new
end
def create
@project = params[:project_id]
@section = @project.sections.build section_params
respond_to do |format|
if @section.save
format.html { redirect_to project_path(@section), :notice => 'Section was successfully created.' }
format.xml { render :xml => @section, :status => :created, :location => @section }
else
format.html { render :action => "new" }
format.xml { render :xml => @section.errors, :status => :unprocessable_entity }
end
end
end
private
def section_params
params.require(:section).permit(:name)
end
new.html.haml
= simple_form_for [@project, @section] do |f|
= f.input :name
= f.button :submit
我认为您应该更改控制器中的创建操作,如下所示:
def create
@project = current_user.projects.find(params[:project_id])
@section = @project.sections.new section_params
respond_to do |format|
if @section.save
format.html { redirect_to project_path(@project), :notice => "Section for #{@project.name} was successfully created." }
format.xml { render :xml => @project, :status => :created, :location => @section }
else
format.html { render :action => "new" }
format.xml { render :xml => @section.errors, :status => :unprocessable_entity }
end
end
end
你对哪个项目有这个表格
= simple_form_for [@project, @section] do |f|
= f.input :name
= f.button :submit
我的意思是,从你得到@project的地方来看,如果你的表单像你在new.html.haml中说的那样处于单独的视图中,那么你的新方法也必须有@project
def new
@section = Section.new
@project = current_user.projects.find(:id) # pass an id for which u are creating section
end
创建操作中的
@project
变量的内容是什么?@TomášDundáček我以为这样做是为了将项目传递到下一行?谢谢您的回复。不幸的是,这返回了一个错误,“找不到没有ID的项目”,我的路线是“/projects/1/sections/new”,谢谢,再加上@Mattherick,这就完成了任务。干杯