简体   繁体   English

没有路由匹配 [GET] "/zombie"

[英]No route matches [GET] "/zombie"

Earlier it was working fine.早些时候它工作正常。 I don't know what happen but I get this error now.我不知道会发生什么,但我现在收到此错误。

Rails.root: /Users/abhimanyuaryan/RubymineProjects/TwitterBy Rails.root: /Users/abhimanyuaryan/RubymineProjects/TwitterBy

在此处输入图片说明

The path in the error message is "/zombie" but all of your paths use "/zombies"错误消息中的路径是“/zombie”,但您的所有路径都使用“/zombies”

you might want to modify the various id paths /zombies/:id(.format) to be /zombie/id(.format).您可能希望将各种 id 路径 /zombies/:id(.format) 修改为 /zombie/id(.format)。 that's matches rails way of doing thing.这符合 Rails 的做事方式。

hth

As mentioned in the answer, your problem is that you're trying to access /zombie when the routes are either /zombies or /zombie/:id .正如答案中提到的,您的问题是当路由是/zombies/zombie/:id时,您正在尝试访问/zombie


I wanted to add that if you're a beginner, the routes system can seem a little confusing...我想补充一点,如果你是初学者,路线系统可能看起来有点混乱......

在此处输入图片说明

The way to understand it is that Rails catches URL routes you send it.理解它的方法是Rails捕获您发送它的URL 路由 Rails is not magic, it's built on top of the HTTP protocol , and as such you can only send urls to your app, such as /zombies or /zombie/:id Rails 并不神奇,它建立在HTTP 协议之上,因此您只能向您的应用程序发送url ,例如/zombies/zombie/:id

Rails uses the ActiveDispatch middleware to take the URL's coming to the app, and routing them to a controller/action , where your code resides. Rails 使用ActiveDispatch 中间件来获取到达应用程序的 URL,并将它们路由到您的代码所在的controller/action Rails then takes the rendered HTML from your code and returns it to the browser, allowing the user to interact with their data etc. Rails 然后从您的代码中获取呈现的 HTML 并将其返回给浏览器,从而允许用户与其数据等进行交互。

The important thing to note is that Rails can only work with what you send it .需要注意的重要一点是Rails 只能处理您发送的内容

The Routes documents are a good step on seeing how this works; Routes 文档是了解其工作原理的良好步骤; ultimately, you have to understand that you're responsible for sending the right URL's to rails, which can be achieved quite simply with thepath helpers .最终,您必须了解您负责向 rails 发送正确的 URL,这可以通过path helpers轻松实现。


Objects对象

Finally, I also wanted to showcase something else - object orientation .最后,我还想展示其他东西—— 面向对象

在此处输入图片说明

Rails is built on Ruby , which makes both of them object orientated . Rails 建立在Ruby之上,这使得它们都面向对象 This is a programming pattern made popular with video games.这是一种在视频游戏中流行的编程模式。

The counter to object orientated programming is flow based programming , which puts the flow of an application at the center of design.面向对象编程的反面是基于流的编程,它将应用程序的置于设计的中心。 Typical "native" apps are flow-based, whilst games are object orientated.典型的“原生”应用程序是基于流程的,而游戏是面向对象的。

The technical difference between the two is that object orientated programs store a series of "objects" (variables) in memory, allowing the user to "interact" with them.两者之间的技术区别在于,面向对象的程序在内存中存储了一系列“对象”(变量),允许用户与它们“交互”。

Flow based design may keep just as much data in memory, but will only let the user access it through certain flow interactions.基于流的设计可能会在内存中保留同样多的数据,但只会让用户通过某些流交互来访问它。 The best example of this would be the likes of setup/installation applications.最好的例子是设置/安装应用程序之类的。

Anyway, because Ruby is object orientated, Rails has been designed to be such as well.无论如何,因为Ruby是面向对象的,所以 Rails 也被设计为面向对象。 Once you understand this, everything becomes much simpler.一旦你理解了这一点,一切就变得简单了。

Instead of thinking of Rails as controllers/variables/data, you want to think in terms of objects -- which object am I interacting with?与其将 Rails 视为控制器/变量/数据,不如从对象的角度来考虑——我要与哪个对象进行交互?

This is why most of Rails is built in a certain way -- to make the manipulation of objects work much smoother:这就是为什么大多数 Rails 都以某种方式构建的原因——使对象的操作工作更加顺畅:

#config/routes.rb
resources :zombies #-> provides routes to interact with Zombie objects

#app/controller/zombies_controller.rb
class ZombiesController < ApplicationController
   def index
      @zombies = Zombie.all #-> show ALL zombie objects
   end

   def show
      @zombie = Zombie.find params[:id] #-> find a single zombie object
   end

   def new
      @zombie = Zombie.new #-> new zombie object
   end

   def create
      @zombie = Zombie.new zombie_params
      @zombie.save #-> save the new Zombie object
   end

   private

   def zombie_params
      params.require(:zombie).permit(:zombie, :params)
   end
end

#app/models/zombie.rb
class Zombie < ActiveRecord::Base
   has_many :enemies #-> each zombie object has many enemies
end

See how it all fits together?看看它们是如何组合在一起的?

If you focus on the object rather than the flow, it becomes much simpler.如果你专注于对象而不是流程,事情就会变得简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM