2016-11-22 48 views
1

我正在创建一个操作,用户可以将作业标记为完成。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.homework_students.each do |homework_student| %> 
      <%= link_to "complete", complete_homework_path(:homework_id => @homework, :home_work_students_id => homework_student), method: :patch %> 
      <% end %> 

我想一个补丁的方法,但它不能正常工作。我不确定是否更好地使用布尔属性,但我不知道如何。家庭作业有很多用户,每个用户都会更新他们是否完成。

错误:

undefined local variable or method `homework 

非常赞赏任何指导。

UPDATE: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"} 

查看更新

<% @homework.homework_students.each do |homework_student| %> 
       <%= link_to "complete", complete_homework_path(@homework, homework_student), method: :patch %> 
       <% end %> 
+0

它看起来像它来自你的link_to你参考homework.id。应该是@ homework.id?很难说没有看到更多的代码,但这个变量似乎没有被定义在任何地方。如果它在你的控制器中,它应该是一个实例变量来在你的视图中访问它。当你点击完整的链接时,你的日志/堆栈跟踪是什么意思? – user3366016

+0

嗨,我无法到达链接,因为该页面无法加载。我把@ homework.id放入如下:<%= link_to“标记为完成”,complete_homework_homework_student_path(@ homework_students,@ homework.id),方法:: patch%>,这是错误...没有路由匹配{: action =>“complete”,:controller =>“homework_students”,:homework_id => nil,:id =>“78”}缺少必需的键:[:homework_id] ...我没有homework_students控制器,只有家庭作业控制器。我会发布一些代码。 – Co2

+0

移动'member do 补丁:完成 结束'一块。你把它嵌套在homework_students控制器中。然后运行'rake routes'来获得正确的路径。另请观看参数。 @homework_students是零(以错误的顺序)。你可能在你的视图中遍历这些内容,这可能是一个局部变量'homework_students.id'。但是,再次,有点难以分辨。 – user3366016

回答

1

我嘲笑根据我们的看法这个版本了,它工作在我结束尽管它可能不是最好的方法。您可能想要观看this video from Mackenzie Child谁使用Ajax在他的待办事项应用程序上进行此类更新(将事情标记为完整等)。

在你的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 %> 
+0

谢谢!对此,我真的非常感激。不幸的是,在尝试你的答案后,我仍然得到未定义的局部变量或方法'homework_student'。表名是homework_students。我在我的控制器中忽略了某些东西,或者看起来很基本我其实正在制作视频。 – Co2

+0

我真的很感激时间,我想我差不多有这样的模型协会是正确的,我错过了一些简单的东西。 – Co2

+0

Np。在这里你说homework_student是未定义的,但在你上面说homework_students。你可以发布完整的错误作为你的问题和视图代码的更新吗? – user3366016