ParticipantsController 中的 NoMethodError#new
NoMethodError in ParticipantsController#new
为什么会出现这个错误?
我的问题还没有 adquate/similar 解决方案。我只能在这里找到一些提示和技巧,但现在我被卡住了。
我们有课程管理系统。您可以添加新的课程、参与者和人员等。我不得不更改数据库。现在还有一个人table。早些时候所有关于个人和参与者的信息刚刚保存在 participants table 中。现在,当添加 新参与者 时,人 table 就会参与进来。
我们想添加课程的新参与者。我在参与者控制器中调整了 new action,我希望像以前那样传递所有数据。旧方法是添加新参与者。
之前的方式是:课程>新学员表格
现在是:课程>找人用>新学员表格
我认为(接受更好的方法)我只是调整旧代码?!下面是我的尝试。
错误
NoMethodError in ParticipantsController#new undefined method `participants' for []:Array
发生。
这里是旧的类:
示范课程
class Course < ActiveRecord::Base
has_many :participants
模范参与者
class Participant < ActiveRecord::Base
belongs_to :course
belongs_to :organization
belongs_to :function
参与者控制器
class ParticipantsController < ApplicationController
....
def new
@course = Course.find(params[:course_id])
@participant = @course.participants.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @participant }
end
end
def create
@course = Course.find(params[:course_id])
@participant = @course.participants.new(params[:participant])
@course.updated_by = current_user.cn
@course.send(:create_version)
@course.tag_version(t(:participant_added))
@course.save!
respond_to do |format|
if @participant.save
format.html { redirect_to course_path(@participant.course), notice: 'Participant was successfully created.' }
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
当您查看下方的课程视图片段时,可以看到表单的新旧路径。请注意,人员搜索现在位于课程和新参与者表单之间。
**old** <%= link_to t(:add), new_course_participant_path(@course) %>
**new** <%= link_to t(:add), course_persons_path(@course, @person)%>
这是新的类
class Participant < ActiveRecord::Base
belongs_to :course
belongs_to :function
belongs_to :person
class Person < ActiveRecord::Base
has_many :participants
has_many :courses, through: :participants
这是我在 ParticipantsController 中的调整。我的想法可能很幼稚,因为我还在 ruby rails 上学习。
class ParticipantsController < ApplicationController
def new
@person = Person.find(params[:person_id])
@participant = Participant.find_by_person_id(params[:person_id])
@course= Course.find(:all, :conditions => {:id => @participant})
@participant = @course.participants.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @participant }
end
end
def create
@course= Course.find(params[:course_id])
@participant = @course.participants.new(params[:participant])
@course.updated_by = current_user.cn
@course.send(:create_version)
@course.tag_version(t(:participant_added))
@course.save!
respond_to do |format|
if @participant.save
format.html { redirect_to course_path(@participant.course), notice: 'Participant was successfully created.' }
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
提前致谢
重写:
@person = Person.find(params[:person_id])
@participant = Participant.find_by_person_id(params[:person_id])
@course= Course.find(:all, :conditions => {:id => @participant})
@participant = @course.participants.build
收件人:
@person = Person.find(params[:person_id])
@course = Participant.find_by_person_id(params[:person_id]).course
@participant = @course.participants.build
注意异常,以防 Participant.find_by_person_id(params[:person_id])
returns 为 nil
为什么会出现这个错误?
我的问题还没有 adquate/similar 解决方案。我只能在这里找到一些提示和技巧,但现在我被卡住了。
我们有课程管理系统。您可以添加新的课程、参与者和人员等。我不得不更改数据库。现在还有一个人table。早些时候所有关于个人和参与者的信息刚刚保存在 participants table 中。现在,当添加 新参与者 时,人 table 就会参与进来。 我们想添加课程的新参与者。我在参与者控制器中调整了 new action,我希望像以前那样传递所有数据。旧方法是添加新参与者。
之前的方式是:课程>新学员表格
现在是:课程>找人用>新学员表格
我认为(接受更好的方法)我只是调整旧代码?!下面是我的尝试。
错误
NoMethodError in ParticipantsController#new undefined method `participants' for []:Array
发生。
这里是旧的类:
示范课程
class Course < ActiveRecord::Base
has_many :participants
模范参与者
class Participant < ActiveRecord::Base
belongs_to :course
belongs_to :organization
belongs_to :function
参与者控制器
class ParticipantsController < ApplicationController
....
def new
@course = Course.find(params[:course_id])
@participant = @course.participants.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @participant }
end
end
def create
@course = Course.find(params[:course_id])
@participant = @course.participants.new(params[:participant])
@course.updated_by = current_user.cn
@course.send(:create_version)
@course.tag_version(t(:participant_added))
@course.save!
respond_to do |format|
if @participant.save
format.html { redirect_to course_path(@participant.course), notice: 'Participant was successfully created.' }
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
当您查看下方的课程视图片段时,可以看到表单的新旧路径。请注意,人员搜索现在位于课程和新参与者表单之间。
**old** <%= link_to t(:add), new_course_participant_path(@course) %>
**new** <%= link_to t(:add), course_persons_path(@course, @person)%>
这是新的类
class Participant < ActiveRecord::Base
belongs_to :course
belongs_to :function
belongs_to :person
class Person < ActiveRecord::Base
has_many :participants
has_many :courses, through: :participants
这是我在 ParticipantsController 中的调整。我的想法可能很幼稚,因为我还在 ruby rails 上学习。
class ParticipantsController < ApplicationController
def new
@person = Person.find(params[:person_id])
@participant = Participant.find_by_person_id(params[:person_id])
@course= Course.find(:all, :conditions => {:id => @participant})
@participant = @course.participants.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @participant }
end
end
def create
@course= Course.find(params[:course_id])
@participant = @course.participants.new(params[:participant])
@course.updated_by = current_user.cn
@course.send(:create_version)
@course.tag_version(t(:participant_added))
@course.save!
respond_to do |format|
if @participant.save
format.html { redirect_to course_path(@participant.course), notice: 'Participant was successfully created.' }
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
提前致谢
重写:
@person = Person.find(params[:person_id])
@participant = Participant.find_by_person_id(params[:person_id])
@course= Course.find(:all, :conditions => {:id => @participant})
@participant = @course.participants.build
收件人:
@person = Person.find(params[:person_id])
@course = Participant.find_by_person_id(params[:person_id]).course
@participant = @course.participants.build
注意异常,以防 Participant.find_by_person_id(params[:person_id])
returns 为 nil