RoR:嵌套表单和修补程序

时间:2016-11-29 19:33:00

标签: ruby-on-rails ruby form-for fields-for

我的应用程序中有一个表单,我希望更新一个名为" interest"的列中的字段。使用另一个名为" homework_students"所以我正在使用(或尝试!)嵌套表格:

    <%= 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

我正在尝试Patch,所以在我的路线中:

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

但它似乎没有使用&#34; update_interest&#34;行动。我也没有homework_students控制器。我得到了这个错误,但上面也可能更加错误。

undefined local variable or method `homework_student'

感谢。感谢任何指导。可以提供更多信息。

1 个答案:

答案 0 :(得分:1)

您的模型文件名似乎不正确。(homework_students.rb) 它应该是单数的(homework_student.rb)。您的模型类名称也将是单数。