2017-10-12 66 views
0

为什么错误会发生?参与者控制器中的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 

在此先感谢

+0

你'@course因为'Course.find(:all,:conditions => {:id => @participant})'是一个空数组,无法从中访问参与者。 –

+0

谢谢你的回复。但为什么id在旧代码中工作? –

+0

您使用的是哪种Rails版本? –

回答

0

重写:

@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])返回零

+0

谢谢!它现在有效。 –

+0

@MarcelB顺便说一句,你真的需要这个'@ person'对象吗?你在什么地方使用它?我认为不需要加载它 – AntonTkachov

+0

我使用表单中的人。就像自动填充功能一样。如果授权用户想要添加新的参与者,则用户可以搜索已经存在的人员。如果一个人已经以我加载搜索到的人的形式作为系统中的参与者存在。表单中的字段是'readonly:true'。像这样:<%= f.text_field:prename,value:@ person.prename,只读:true,style:'background-color:#BDBDBD',size:35%>' –