В приложении My Rails для студента-планировщика есть несколько проблем, связанных с фальсификацией URL. Я полагаю, что они, вероятно, все используют одно и то же решение, но у меня возникают трудности.
При просмотре задания (students/:id/assignments/:id
) иногда меняют идентификатор назначения в URL-адресе на идентификатор задания, принадлежащего другому студенту. приводит к «ошибке метода» на моей странице assignments#show
, в других случаях это покажет назначение другого студента, когда в идеале я бы хотел просто перенаправить обратно на их домашнюю страницу.
Аналогично, это происходит со страницей редактирования задания (students/:id/assignments/:id/edit
), курсом (students/:id/courses/:id
) и страницей редактирования курса (students/:id/courses/:id/edit
). Иногда я получаю «ArgumentError in Assignments#edit
» при просмотре страницы редактирования задания.
Я считаю, что это можно исправить в моих контроллерах, поэтому я включил мои assignments_controller
и courses_controller
.
Assignments_controller:
class AssignmentsController < ApplicationController
before_action :require_logged_in
before_action :set_student
def new
if @student && @student.id == current_student.id
@assignment = Assignment.new
@courses = Course.where(student_id: current_student.id)
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users assignments.'
end
end
def create
@assignment = Assignment.new(assignment_params)
@assignment.student_id = current_student.id if current_student
@courses = Course.where(student_id: current_student.id)
if @assignment.save
redirect_to student_assignments_path(@student)
else
render :new
end
end
def index
if @student && @student.id == current_student.id
@assignments = Assignment.where(student_id: current_student.id)
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users assignments.'
end
end
def show
#student = Student.find_by(id: params[:student_id])
if @student && @student.id == current_student.id
#@assignment = student.assignments.find_by(id: params[:id])
@assignment = Assignment.find_by(id: params[:id])
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users assignments.'
end
end
def edit
if @student && @student.id == current_student.id
@assignment = Assignment.find_by(id: params[:id])
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users assignments.'
end
end
def update
student = Student.find_by(id: params[:student_id])
@assignment = Assignment.find_by(id: params[:id])
@assignment.update(params.require(:assignment).permit(:title, :due_date))
redirect_to student_assignment_path(student, @assignment)
end
def destroy
@student = Student.find_by(id: params[:student_id])
@assignment = Assignment.find_by(id: params[:id]).destroy
redirect_to student_path(@student), notice: 'Assignment was successfully completed.'
end
private
def assignment_params
params.require(:assignment).permit(:title, :due_date, :course_id, :student_id)
end
def set_student
@student = Student.find_by(id: params[:student_id])
end
end
Courses_controller:
class CoursesController < ApplicationController
before_action :require_logged_in
before_action :set_student
def new
if @student && @student.id == current_student.id
@course = Course.new
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users courses.'
end
end
def create
if @student && @student.id == current_student.id
@course = Course.create(course_params)
@course.student_id = params[:student_id]
if @course.save
redirect_to student_courses_path(@student)
else
render :new
end
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users courses.'
end
end
def index
if @student && @student.id == current_student.id
@courses = Course.where(student_id: current_student.id)
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users courses.'
end
end
def show
@student = Student.find_by(id: params[:student_id])
if @student && @student.id == current_student.id
@course = @student.courses.find_by(id: params[:id])
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users courses.'
end
end
def edit
if @student && @student.id == current_student.id
@course = Course.find_by(id: params[:id])
else
redirect_to student_path(current_student), error: 'Sorry, you can\'t view another Users courses.'
end
end
def update
student = Student.find_by(id: params[:student_id])
@course = Course.find_by(id: params[:id])
@course.update(params.require(:course).permit(:course_name))
redirect_to student_course_path(student, @course)
end
def destroy
@student = Student.find_by(id: params[:student_id])
@course = Course.find_by(id: params[:id]).destroy
redirect_to student_path(@student), notice: 'Course was successfully deleted.'
end
private
def course_params
params.require(:course).permit(:course_name)
end
def set_student
@student = Student.find_by(id: params[:student_id])
end
end