gpt4 book ai didi

ruby-on-rails - Rails 路线 : GET method redirecting to show method

转载 作者:行者123 更新时间:2023-12-02 17:31:11 25 4
gpt4 key购买 nike

我有简单的 Controller 和路由文件。在我的路线和 Controller 中,我创建了一个模块。我写了一个简单的方法来重定向我的节目。我不确定为什么。

Controller

  module Seller
class CampaignsController < Seller::BaseController

before_action :confirm_logged_in

def viewAllCampaigns
@campaigns = Campaign.all

end

def show
end

end

end

路线文件

 scope module: 'seller' do
#namespace :power do
resources :dashboard, only: [:index]
resources :sessions, only: [:create, :destroy]
resources :campaigns, only: [:index, :create, :show, :update, :destroy]
get 'viewAllCampaigns' => 'campaigns#viewAllCampaigns'
end

输出

Started GET "/campaigns/viewAllCampaigns" for 127.0.0.1 at 2015-10-12 17:39:43 +0500
Processing by Seller::CampaignsController#show as HTML
Parameters: {"id"=>"viewAllCampaigns"}
Rendered seller/campaigns/show.html.erb (0.1ms)

我正在点击 http://localhost:3000/campaigns/viewAllCampaigns在浏览器中。

最佳答案

理想情况下,您的路由应该这样定义。

resources :campaigns, only: [:index, :create, :show, :update, :destroy] do  
get 'viewAllCampaigns', on: :collection
end

routes.rb 文件的第一个注释是 优先级基于创建顺序:最先创建 -> 最高优先级。 这就是您的路线的原因重定向到 show。 Rails 将此 url 视为 campain/:id

关于ruby-on-rails - Rails 路线 : GET method redirecting to show method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33081621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com