gpt4 book ai didi

ruby-on-rails - 没有路线匹配 - Rails

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

在 Rails 中设置我的第二个项目(首先没有遵循教程)并且遇到了我无法用我的路线解决的问题。我的 routes.rb 文件一切正常,如我所愿:

AnimalApp::Application.routes.draw do

root to: 'static_pages#index'
# resources :animal
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/league', to: 'static_pages#league'
match '/animal', to: 'animal#new'

除了我无法访问我的动物!转到/animal/1 会引发错误:没有路线匹配 [GET] "/animal/1"

为了解决这个问题,我改变了我的路线如下:

    AnimalApp::Application.routes.draw do

root to: 'static_pages#index'
resources :animal
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/league', to: 'static_pages#league'
#match '/animal', to: 'animal#new'

但是,当我尝试查看我网站上的任何其他页面时,出现以下错误:没有路线匹配 {:action=>"show", :controller=>"animal"}

有关更多信息,请参阅下面我的 Controller :(静态页面)

class StaticPagesController < ApplicationController
def help
end

def league
end

def contact
end

def about
end

def index
end

def show
end
end

动物 Controller

class AnimalController < ApplicationController
def new
@animal = Animal.new
end

def show
@animal = Animal.new
end
end

而且,我还运行了 rake routes 我得到:

animal_index GET    /animal(.:format)          animal#index
POST /animal(.:format) animal#create
new_animal GET /animal/new(.:format) animal#new
edit_animal GET /animal/:id/edit(.:format) animal#edit
animal GET /animal/:id(.:format) animal#show
PUT /animal/:id(.:format) animal#update
DELETE /animal/:id(.:format) animal#destroy
root / static_pages#index
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
league /league(.:format) static_pages#league
/animal(.:format) animal#new

有没有人知道如何恢复我的网站并允许我自己在 URI/animal/[:id] 下查看我的动物数据库中的对象?

最佳答案

让它成为 resources :animals 而不是 resources :animal

rake routes 应显示以下输出:

    animals GET    /animals(.:format)          animals#index
POST /animals(.:format) animals#create
new_animal GET /animals/new(.:format) animals#new
edit_animal GET /animals/:id/edit(.:format) animals#edit
animal GET /animals/:id(.:format) animals#show
PUT /animals/:id(.:format) animals#update
DELETE /animals/:id(.:format) animals#destroy

关于ruby-on-rails - 没有路线匹配 - Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990633/

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