我正在创建一个操作,用户可以将作业标记为完整。
作业主要存储在作业表中,但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: :destroyhomework.rb
has_many :homework_students, :class_name => 'HomeworkStudent', dependent: :destroy我的尝试。
在我的作业显示视图中,我希望用户能够单击“完成作业”,并将日期存储在completed_on属性中的homework_students中。
路线:
resources :homeworks do
resources :homework_students do
member do
patch :complete
end
end
endhomeworks_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非常感谢你的指导。
更新: 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 %>发布于 2016-11-23 00:35:28
我根据我们的评论来嘲弄这个版本,虽然它可能不是最好的方法,但它在我的端起作用了。您可能希望看到使用ajax在他的todo应用程序上进行这种更新的这是Mackenzie Child的视频 (将事情标记为完整,等等)。
在你的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.'
endbefore_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 %>https://stackoverflow.com/questions/40752826
复制相似问题