gpt4 book ai didi

ruby-on-rails - 了解铁路路线

转载 作者:数据小太阳 更新时间:2023-10-29 07:55:12 30 4
gpt4 key购买 nike

我对 Rails 路由的工作原理感到非常困惑。此时我有一个非常简单的应用程序,因此应该很容易调试。这是我的问题 - 我有一个 Controller 试图调用一个显示 Action ,但它似乎覆盖了我在同一个 Controller 中调用的另一个 Action 。据我所知,rake routes 是,如果 url 读取此 localhost.com/subscribers/1 应该呈现我想查看的订阅者的 id,但我也有一个像这样调用的 url localhost.com/subscribers/visit 这么有效,这些页面现在都不起作用了吗?我真的不知道这里发生了什么,所以任何帮助都会很棒!这是我的代码。

Controller

   class SubscribersController < ApplicationController
helper_method :sort_column, :sort_direction

def index
@search = Subscriber.search(params[:q])
@subscriber = @search.result
@search.build_condition if @search.conditions.empty?
@search.build_sort if @search.sorts.empty?
end

def show
@subscriber = Subscriber.find_by(id: params[:id])
end

def new
@subscriber = Subscriber.new
end

def create
@subscriber = Subscriber.new(subscriber_params)
if @subscriber.save
@subscriber.touch(:subscription_date)
SubscriberMailer.welcome_subscriber(@subscriber).deliver_now
flash[:notice] = "Subscriber Has Been Successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end

def search
@subscriber = Subscriber.new
end

def visit
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit ||= 0
@subscriber.visit += 1
@subscriber.save
render "visit"
end
end

路线:

    devise_for :users
resources :subscribers
resources :comments, only: [:new, :create]

get "subscribers/search", to: "subscribers#search"
get "subscribers/visit", to: "subscribers#visit"

root "subscribers#new"

RAKE 路线:

subscribers GET    /subscribers(.:format)             subscribers#index
POST /subscribers(.:format) subscribers#create

new_subscriber GET /subscribers/new(.:format) subscribers#new
edit_subscriber GET /subscribers/:id/edit(.:format) subscribers#edit
subscriber GET /subscribers/:id(.:format) subscribers#show
PATCH /subscribers/:id(.:format) subscribers#update
PUT /subscribers/:id(.:format) subscribers#update
DELETE /subscribers/:id(.:format) subscribers#destroy
comments POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
subscribers_search GET /subscribers/search(.:format) subscribers#search
subscribers_visit GET /subscribers/visit(.:format) subscribers#visit

我在这里做错了什么吗?请帮帮我!

最佳答案

路由器尝试匹配从 routes.rb 顶部到底部的路由,并使用第一个匹配的路由。

在您的情况下,/subscribers/visit 匹配 /subscribers/:id - 路由器认为这是 ID 为 visit< 的订阅者的显示操作.

如果您将自定义路径移动到路由文件中的 resources :subscribers 上方,您将实现预期的行为。

关于ruby-on-rails - 了解铁路路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175180/

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