diff --git a/app/controllers/talks_controller.rb b/app/controllers/talks_controller.rb index 65a9bd74..0973b059 100644 --- a/app/controllers/talks_controller.rb +++ b/app/controllers/talks_controller.rb @@ -43,7 +43,9 @@ def update # Use callbacks to share common setup or constraints between actions. def set_talk - @talk = Talk.includes(:speakers, :event).find_by!(slug: params[:slug]) + @talk = Talk.includes(:speakers, :event).find_by(slug: params[:slug]) + + redirect_to talks_path, status: :moved_permanently if @talk.blank? end # Only allow a list of trusted parameters through. diff --git a/test/controllers/talks_controller_test.rb b/test/controllers/talks_controller_test.rb index 004bd9fd..3e1812c0 100644 --- a/test/controllers/talks_controller_test.rb +++ b/test/controllers/talks_controller_test.rb @@ -15,6 +15,11 @@ class TalksControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test "should redirect to talks for wrong slugs" do + get talk_url("wrong-slug") + assert_response :moved_permanently + end + test "should get edit" do get edit_talk_url(@talk) assert_response :success