:更新表单不保存布尔值

时间:2016-06-17 22:00:03

标签: ruby-on-rails ruby boolean

我是一名初学者,为我的语言学生构建一个应用程序,用于检查用户是否正确地将术语复制到表单中。 Word模型具有:term属性,这是用户将要复制的内容。 Word Exposition模型与Word模型相关联;在注册时,每个WordExposition-Word关联的:completed属性设置为false。我正在尝试在WordExposition show视图中实现一个表单,这样如果用户键入正确的术语,WordExposition completed属性将从默认的false更改为true。

为了检查Word.term是否与学生复制的术语匹配,我在WordExposition模型中有一个word_from_student_matches_word方法,我希望在更新之前运行该方法。截至目前,我在提交表格时收到undefined local variable or method word_from_student_matches_word'`。我怎样才能检查匹配的拼写并从视图中更新布尔属性?

WordExposition模型:

class WordExposition < ActiveRecord::Base
  belongs_to :enrollment
  belongs_to :word

  delegate :term, to: :word 
  delegate :reference, to: :word
  delegate :image, to: :word
  delegate :sound, to: :word

  attr_accessor :term_given_by_student
  validate :word_from_student_matches_word, on: :update

  def word_from_student_matches_word
    return true if word.term == term_given_by_student
    errors.add(:term_given_by_student, "Terms don't match")
  end

  def next_word_exposition
    WordExposition.where(["id > ? AND lesson_id = ?", id, lesson_id]).first
  end
end

Word Expositions控制器:

class WordExpositionsController < ApplicationController
  before_action :authenticate_user!
  before_action :require_enrollment_in_lesson

  def show
    @word = current_enrollment.word_expositions.find_by!(word_id: params[:id])
  end

  def update
    current_word_exposition 
    if word_from_student_matches_word
      current_word_exposition.completed = true
      current_word_exposition.save
    end
  end

  private

  helper_method :current_lesson
  def current_lesson
    @current_lesson ||= Lesson.find(params[:lesson_id])
  end

  helper_method :current_enrollment
  def current_enrollment
    @current_enrollment ||= Enrollment.find_by!(lesson_id: params[:lesson_id], user_id: current_user.id)
  end

  def require_enrollment_in_lesson
    if !(current_user.enrolled_in?(current_lesson))
      redirect_to lesson_path(current_lesson), alert: 'You need to enroll in order to view the activities!'
    end
  end

  def word_exposition_params
    params.require(:word_exposition).permit(:completed)
  end

  def current_word_exposition
    @current_word_exposition ||= WordExposition.find(params[:id])
  end
end

Word Expositions显示视图:

<h1>Word Exposition</h1>

<!-- display the term to be copied -->
<div class="col-xs-10 col-xs-offset-1">
  <h2><%= @word.term %></h2><br>

  <!-- Form to check matching spelling and update WordExposition :completed to true if correct -->
  <%= simple_form_for @word, url: lesson_word_exposition_path(current_lesson, @word), method: :patch do |f| %>
    <%= f.input :term_given_by_student, label: "Enter the term exactly as above:" %><br>
    <%= f.button :submit, class: 'btn btn-primary' %>
  <% end %>
</div>

耙路线:

      lesson_enrollments POST   /lessons/:lesson_id/enrollments(.:format)            enrollments#create
  lesson_word_exposition GET    /lessons/:lesson_id/word_expositions/:id(.:format)   word_expositions#show
                         PATCH  /lessons/:lesson_id/word_expositions/:id(.:format)   word_expositions#update
                         PUT    /lessons/:lesson_id/word_expositions/:id(.:format)   word_expositions#update
                 lessons GET    /lessons(.:format)                                   lessons#index
                  lesson GET    /lessons/:id(.:format)                               lessons#show
                    word GET    /words/:id(.:format)                                 words#show
    teacher_lesson_words POST   /teacher/lessons/:lesson_id/words(.:format)          teacher/words#create
 new_teacher_lesson_word GET    /teacher/lessons/:lesson_id/words/new(.:format)      teacher/words#new
edit_teacher_lesson_word GET    /teacher/lessons/:lesson_id/words/:id/edit(.:format) teacher/words#edit
     teacher_lesson_word PATCH  /teacher/lessons/:lesson_id/words/:id(.:format)      teacher/words#update
                         PUT    /teacher/lessons/:lesson_id/words/:id(.:format)      teacher/words#update
                         DELETE /teacher/lessons/:lesson_id/words/:id(.:format)      teacher/words#destroy
         teacher_lessons POST   /teacher/lessons(.:format)                           teacher/lessons#create
      new_teacher_lesson GET    /teacher/lessons/new(.:format)                       teacher/lessons#new
     edit_teacher_lesson GET    /teacher/lessons/:id/edit(.:format)                  teacher/lessons#edit
          teacher_lesson GET    /teacher/lessons/:id(.:format)                       teacher/lessons#show
                         PATCH  /teacher/lessons/:id(.:format)                       teacher/lessons#update
                         PUT    /teacher/lessons/:id(.:format)                       teacher/lessons#update
                         DELETE /teacher/lessons/:id(.:format)                       teacher/lessons#destroy

1 个答案:

答案 0 :(得分:0)

你的控制器错了

def update
  current_word_exposition
  current_word_exposition.completed = true #this line move to callback in your model changing the flag before save
  if current_word_exposition.save
    #nothing o something
  else
    #do something
  end
end

您的模型正在验证更新时的术语,如果出现问题则返回false。只需要保存模型实例并控制结果