RoR:标记对象完成,将completed_on保存在其他模型中

我正在创建一个动作,用户可以将作业标记为完整。

家庭作业主要存储在家庭作业表中,但homework_student包含completed_on和布尔完整属性:

homework_student.rb

id :integer not null, primary key # school_user_id :integer not null # homework_id :integer not null # completed_on :datetime # created_at :datetime not null # updated_at :datetime not null # complete :boolean belongs_to :homework, :class_name => 'Homework', :foreign_key => :homework_id, dependent: :destroy 

homework.rb

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

我的尝试……

在我的家庭作业展示视图中,我希望用户能够点击完成他们的作业,并将日期存储在homework_students中的completed_on属性中。

路线:

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

homeworks_controller.rb:

 def complete @complete_item = Homework.homework_students.update_attribute(:completed_on, Time.now) end 

在我看来:

   @homework, :home_work_students_id => homework_student), method: :patch %>  

我正在尝试PATCH方法,但它无法正常工作。 我不确定为此使用布尔属性是否更好,但我不确定如何。 家庭作业有许多用户,每个用户都会更新他们是否完成。

错误:

 undefined local variable or method `homework 

非常感谢任何指导。

更新:homeworks_controller.rb

  before_action :find_homework, only: [:show, :complete, :edit, :update] #before_action :set_homework, only: [:show] #before_action :set_homework_student, except: [:create] before_action :authenticate_user! .... def complete # Loop over each matching homework_student for the @homework where the id matches and update the completed_on attribute. # You need to get the indivual homework_student record and update it. You could do it other ways but this seemed to work for me. @homework.homework_students.where(id: params[:home_work_students_id]).each do |homework_student| homework_students.update_attributes(:completed_on => Time.now) end # Redirect back to the @homework view. redirect_to @homework, notice: 'Homework was successfully marked as complete.' end .... private def set_homework_student @homework_students = HomeworkStudent.find(params[:homework_id]) end def set_homework @homework = @homework_students.homeworks.find(params[:id]) end def homework_params params.require(:homework).permit(:user_id, :id, :school_user_id, :homeworks, :significance, :significance_id, :sig_option, :feedback_request, :subject, :source, :description, :due, :completed_at, homework_students: [:completed_on, :complete], school_user: [:f_name, :s_name]) end def find_homework @homework = Homework.find(params[:id]) end end 

更新错误:

 Error message: undefined method `update_attributes' for nil:NilClass Rails.root: Application Trace | Framework Trace | Full Trace app/controllers/homeworks_controller.rb:39:in `block in complete' app/controllers/homeworks_controller.rb:39:in `complete' Request Parameters: {"_method"=>"patch", "authenticity_token"=>"OL1vW0BnnJ8TM5lshZWNKisNMrTU2Gp2cY7jYf3T+NRhlx2994q0ndrpehz+vW5unY1EBtSKCHecOJjTDwLHsw==", "home_work_students_id"=>"142", "homework_id"=>"78", "id"=>"78"} 

查看更新

     

我根据我们的评论嘲笑了这个版本,虽然它可能不是最好的方法,但它在我的最后工作。 您可能希望观看来自Mackenzie Child的video,该video使用ajax在他的todo应用程序上进行此类更新(将内容标记为完整等)。

在您的routes.rb中

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

在你的homework_controller.rb中

  def complete # Loop over each matching homework_student for the @homework where the id matches and update the completed_on attribute. # You need to get the indivual homework_student record and update it. You could do it other ways but this seemed to work for me. @homework.homework_students.where(id: params[:home_work_students_id]).each do |homework_student| homework_student.update_attributes(:completed_on => Time.now) end # Redirect back to the @homework view. redirect_to @homework, notice: 'Homework was successfully marked as complete.' end 

before_action :set_homework, only: [:show, :complete]

在你的作业show.html.erb

 # This assumes you are looping over the @homework.homework_students in your view to create a link for each homework_student record and that will give you access to the homework_student local variable. <%= link_to "complete", complete_homework_path(:homework_id => @homework, :home_work_students_id => homework_student), method: :patch %>