2016-06-17 108 views
1

我是一名初學者,爲我的語言學生構建一個應用程序,檢查用戶是否將表單正確地複製到表單中。 Word模型具有:term屬性,這是用戶將要複製的內容。 Word展示模型與Word模型相關聯;在註冊時,每個WordExposition-Word關聯的:completed屬性設置爲false。我試圖在WordExposition顯示視圖中實現一個窗體,這樣如果用戶輸入正確的術語,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 

字表演控制器:

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 

字表演放映視圖:

<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 
+0

在您的控制器中調用'word_from_student_matches_word',但該方法在模型實例上定義。 –

回答

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,如果出事了。只需要保存模型實例並控制結果

+0

我添加了其他警報,即使條款匹配也會顯示。這意味着驗證未運行,並且更新未保存。任何想法如何解決這個問題? Gracias por la ayuda! – gonzalo2000

+0

因爲您沒有填充模型實例的新值,current_word_exposition.term = params [:word] [:term]或current_word_exposition.update(params),並且您的word_exposition_params方法也是錯誤的,所以它必須允許:term_given_by_student而不是:completed – DennisCastro

+0

如果我的WordExposition模型僅包含a:completed布爾參數,那麼這仍然適用嗎?我不想傳遞表單中的單詞,只是爲了在條款匹配時將其更新爲true。 – gonzalo2000