为什么错误会发生?参与者控制器中的NoMethodError#new
有没有一个适合我的问题的解决方案。我只能在这里找到一些提示和技巧,但现在我陷入困境。
我们有一个课程管理系统。您可以添加新的coures,参与者和其他人。我不得不改变数据库。现在还有一个人表。早些时候所有关于参与人员的信息都保存在参与者表中。现在,当新参与者被添加时,涉及人表。 我们想添加一个课程的新参与者。我调整了参与者控制器中的新动作,并且希望以旧方式传递所有数据。旧的方式正在努力添加一个新的参与者。
早些时候的方式是:当然>新的参与形式
现在是:当然>寻找一个人的形式使用>新的参与形式
我想(接受更好的方法)我只是调整旧代码?!以下是我的尝试。
在ParticipantsController#的误差
NoMethodError新未定义的方法`参与者的[]:阵列发生
。
这里是老班:
示范课程
class Course < ActiveRecord::Base
has_many :participants
型号参与者
class Participant < ActiveRecord::Base
belongs_to :course
belongs_to :organization
belongs_to :function
ParticipantsController
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 on 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
在此先感谢
你'@course因为'Course.find(:all,:conditions => {:id => @participant})'是一个空数组,无法从中访问参与者。 –
谢谢你的回复。但为什么id在旧代码中工作? –
您使用的是哪种Rails版本? –