0
更新後にユーザーを次のWordExpositionモデルにリダイレクトしようとしています。私は現在すぐ隣にあるword_exposition idにはうまくいきますが、次のレッスンのword_expositionのIDがスキップすると、RecordNotFound
が返されます(つまり、IDが1-4の間で正しくリダイレクトされますが、次のIDが6の場合には破損します)。同じレッスンに属するWordExposition以外のインスタンスに対しても、どのようにリダイレクトすることができますか?更新時に次のインスタンスにリダイレクト
私はthis投稿のアイデアのnext_exposition
モデルメソッドを基にしていますが、ここでは動作させるために何かが欠けています。
WordExpositionモデル:
class WordExposition < ActiveRecord::Base
belongs_to :enrollment
belongs_to :word
def next_exposition
WordExposition.where(["id > ? AND enrollment_id = ?", id, enrollment_id]).first
end
end
WordExpositionsコントローラ:
class WordExpositionsController < ApplicationController
def update
current_word_exposition
@current_word_exposition.completed = true
@current_word_exposition.term_given_by_student = params[:word_exposition][:term_given_by_student]
if @current_word_exposition.save
flash[:notice] = "Congratulations!"
#currently only redirects correctly for adjacent words in the same lesson, should do so for non-adjacent word_expositions in the same lesson
if next_word = @current_word_exposition.next_exposition
redirect_to lesson_word_exposition_path(current_lesson, next_word)
end
else
flash[:alert] = "Enter the word exactly as shown!"
redirect_to lesson_word_exposition_path(current_lesson, current_word_exposition)
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 word_exposition_params
params.require(:word_exposition).permit(:completed)
end
helper_method :current_word_exposition
def current_word_exposition
@current_word_exposition ||= current_enrollment.word_expositions.find_by!(word_id: params[:id])
end
end
を試すことができます。これは、フォームの送信に例外が発生します: 'ActiveRecordの:: StatementInvalidでWordExpositionsController#update' 'SQLite3 :: SQLException:そのようなテーブルはありません:items:SELECT" word_expositions "。* FROM" word_ (id = 2の項目からmin(id)を選択してください)ORDER BY "word_expositions"。 "id" ASC LIMIT 1'(私は助けてくれてありがとうございます) – gonzalo2000
今すぐお試しください答えをコピーして、エラーが何であるかを見て、 'items'はテーブルではない、私は私のDBにあった' Item'テーブルに対してこのクエリを実行しました。 'word_expositions'はありませんでした。エラーでそれを修正してみてください – Saad
私は理解しています - 手に感謝!テーブル内の次のものにスキップするのではなく、インスタンスを持たないword_expressionをレンダリングしようとしています( '[WHERE" word_expositions "。" enrollment_id "=?AND" word_expositions "でWordExpositionを見つけることができませんでした。 "=?]') – gonzalo2000