爲什麼錯誤會發生?參與者控制器中的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版本? –