2016-11-29 79 views
0

我在我的應用程序中有一個表單,我正在使用另一個名爲「homework_students」的模型中的單選按鈕來更新名爲「interest」的列中的字段,所以我正在使用(或嘗試!)嵌套形式:RoR:嵌套窗體和補丁

<%= form_for @homework, url: homework_homework_student_path(:homework_id => @homework, home_work_students_id => @homework_student, :controller => 'homeworks', :action => 'update_interest'), method: :patch do |f| %> 

    <%= f.fields_for :homework_students do |builder| %> 

    <%= builder.radio_button :interest, '1' %> 
    </label> 
    1 
    <label> 
    <%= builder.radio_button :interest, '2' %> 
    </label> 
    2 
    <label> 
    <%= builder.radio_button :interest, '3' %> 
    3 
    </label> 
<%= f.button :submit %> 

<% end %> 
<% end %> 

在我的模型:

homework.rb:

has_many :homework_students, :class_name => 'HomeworkStudent', dependent: :destroy 
accepts_nested_attributes_for :homework_students 

homework_students.rb

belongs_to :homework, :class_name => 'Homework', :foreign_key => :homework_id, dependent: :destroy 

我想在我的控制器使用的動作(homeworks_controller.rb):

def update_interest 
    @homework.homework_students.where(id: params[:home_work_students_id]).each do |homework_student| homework_student.update_attributes(:interest) 
    end 
    redirect_to @homework, notice: 'Homework was successfully updated.' 
end 

我想在這裏補丁所以在我的路線:

resources :homeworks do 
     member do 
     patch :update_interest 
     end 
    resources :homework_students 
    end 

但它似乎並沒有使用「update_interest」操作。我也沒有homework_students控制器。我得到這個錯誤,但也許更多的錯誤以上。

undefined local variable or method `homework_student' 

謝謝。欣賞任何指導。可以提供更多信息。

回答

1

看起來你的模型文件名是不正確的(homework_students.rb) 它應該是單數(homework_student.rb)。你的模型類名也是單數。