rails 6.1.4 -- ruby 2.6.7p197
我正在开发一个小应用程序。到目前为止,它有一个表单,在提交时,它将转到controller#create方法。当记录为created时,会发生thank_you方法的redirect_to。redirect_to不起作用。它实际上是重定向到show方法。我“假设”这是一个路由问题,但我不知道我做错了什么。
我需要一些帮助来理解为什么redirect_to要去show而不是thank_you。
EDIT in response to suggestion by @Vishal Jain below:
I've tried these in the redirect_to:
redirect_to(living_muay_thai_survey_pain_points_thank_you_path)
redirect_to(living_muay_thai_survey_pain_points_thank_you_url)
They both still take me to the show action/view.Controller:
class LivingMuayThai::SurveyPainPointsController < ApplicationController
layout "living_muay_thai"
def index
end
def new
@survey_pain_point = LivingMuayThai::SurveyPainPoint.new
end
def create
pain_point = LivingMuayThai::SurveyPainPoint.new(pain_point_params)
if pain_point.save
flash[:notice]="Your survey has been submitted"
redirect_to(action: "thank_you")
else
flash[:notice]="Sorry, your survey could not be saved."
render
end
end
def thank_you
end
def show
end
...
end
-----------------------------
Routes:
namespace :living_muay_thai do
resources :survey_pain_points do
collection do
get :search
post :search
end
end
end
get 'living_muay_thai/survey_pain_points/thank_you'
post 'living_muay_thai/survey_pain_points/thank_you'
...
routes in command line for thank_you and show.
living_muay_thai_survey_pain_points_thank_you GET /living_muay_thai/survey_pain_points/thank_you(.:format) living_muay_thai/survey_pain_points#thank_you
living_muay_thai_survey_pain_point GET /living_muay_thai/survey_pain_points/:id(.:format) living_muay_thai/survey_pain_points#show我添加了thank_you操作和视图,这样用户就不会进入索引视图。他们不需要看所有的调查记录。我为thank_you添加了namespace和resources之外的路径,因为我无法让它在其中工作。
我看到thank_you路由是复数...pain_points...,节目是单数...pain_point...,我试图使thank_you路由变得奇异,但这是行不通的。
我做错了什么?
谢谢你的帮助。
发布于 2022-05-30 13:25:04
我想出来了。在盯着它看了一个小时,尝试了不同的事情之后,我想起了几年前我读到的一些规则,即路线的顺序是重要的:First one that fits the route needed is used (or similar)。因此,我将thank_you路由移到了命名空间块之前,只是为了尝试一下.:
get 'living_muay_thai/survey_pain_points/thank_you'
namespace :living_muay_thai do
resources :survey_pain_points do
end
end它现在正常工作了。仍然不清楚为什么show被认为适合thank_you的操作,但我确信当我没有考虑到它时,它会出现在我的脑海中。
谢谢你找我。
https://stackoverflow.com/questions/72428217
复制相似问题