首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >路由重定向转到错误操作-视图

路由重定向转到错误操作-视图
EN

Stack Overflow用户
提问于 2022-05-30 00:41:05
回答 1查看 31关注 0票数 0

rails 6.1.4 -- ruby 2.6.7p197

我正在开发一个小应用程序。到目前为止,它有一个表单,在提交时,它将转到controller#create方法。当记录为created时,会发生thank_you方法的redirect_toredirect_to不起作用。它实际上是重定向到show方法。我“假设”这是一个路由问题,但我不知道我做错了什么。

我需要一些帮助来理解为什么redirect_to要去show而不是thank_you

代码语言:javascript
复制
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.
代码语言:javascript
复制
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添加了namespaceresources之外的路径,因为我无法让它在其中工作。

我看到thank_you路由是复数...pain_points...,节目是单数...pain_point...,我试图使thank_you路由变得奇异,但这是行不通的。

我做错了什么?

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2022-05-30 13:25:04

我想出来了。在盯着它看了一个小时,尝试了不同的事情之后,我想起了几年前我读到的一些规则,即路线的顺序是重要的:First one that fits the route needed is used (or similar)。因此,我将thank_you路由移到了命名空间块之前,只是为了尝试一下.:

代码语言:javascript
复制
get 'living_muay_thai/survey_pain_points/thank_you'
namespace :living_muay_thai do
  resources :survey_pain_points do 
  end
end

它现在正常工作了。仍然不清楚为什么show被认为适合thank_you的操作,但我确信当我没有考虑到它时,它会出现在我的脑海中。

谢谢你找我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72428217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档