这是如何发送到 Show 操作的

How is this sent to the Show action

我是 rails 上 ruby 的新手,我想了解这段代码如何重定向到显示操作,谢谢:

def create
  @article = Article.new(params[:article])
  @article.save

  redirect_to @article
end

如果您使用定义 RESTful 路线的 Rails 约定来定义您的路线,即 routes.rb 文件中的 resources :articles,那么 redirect_to @article 将带您到此特定 @article 实例的 show 页面。 Rails 正在发挥潜在的魔力。

当您在 routes.rb 文件中写入 resources :articles 时,Rails 会自动为您生成这些路由:

      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy

所以你有这条映射到 articles 控制器的 show 动作的特定路线:

article GET    /articles/:id(.:format)      articles#show

当您执行以下操作时,此路由匹配:redirect_to @article,这就是将您带到此 @article.

show 页面的原因

要了解有关 RESTful 路由如何工作的更多信息,请参阅 this Rails tutorial

基本上你有以下路线:

Prefix Verb   URI Pattern                  Controller#Action
article GET    /articles/:id(.:format)      articles#show

为了重定向到特定文章的show操作,您需要以'/articles/:id'形式的字符串结尾,并且有几个层次的语法糖完成这个:

redirect_to @article
redirect_to article_path(@article)
redirect_to article_path(@article.id)

您也可以明确指定路径,甚至是完整的 URL:

redirect_to "/articles/#{@article.id}"
redirect_to "http://myapp.com/articles/#{@article.id}"

尽管我不推荐这样做。

有关 redirect_to

的更多使用方法,请参阅 API docs

I am new to ruby on rails

要了解它的工作原理,您需要了解 Ruby 的本质,而不是 Rails。


OOP

因为 Rails 建立在 Ruby 之上,它是 面向对象编程 模式的代表——应用程序的每个元素应该面向 objects:

的创建和操作

尽管您的问题的答案在于 Rails 这种方法的应用,但它的理解始于如何 Ruby 让它工作。

为了获得真正面向对象的体验,Ruby 构建了 classes which are available in memory to manipulate & utilize. These classes are invoked, and have a series of methods,您可以使用它与它们进行交互。

在Rails的意义上(我对Ruby不是很有经验),这些方法有两种形式:

  • Class methods (def self.x)
  • Instance methods (def x)

这些工作使您能够调用这些方法来按照您认为合适的方式操作您的对象。例如,如果您正在创建一个外星人游戏,您可以 @alien.is_visible?

这是Ruby给我们的下划线结构


Rails

因为Rails是建立在Ruby之上的(Ruby是语言,Rails 是框架),你的答案在于 Rails 如何操纵你创建的对象。

要正确理解它,您需要知道 Rails 是一个 MVC (Model View Controller) 框架:

如您所见,Rails 在您的 模型 中构建您的对象。这就是为什么你可以调用 @article = Article.find x.

因此,当您询问 redirect_to @article 是如何工作时,您需要记住 Rails 以某种方式构建其对象(IE 具有一系列 base 方法 - 继承自 ActiveRecord::Base).

这些方法为每个模型 对象 提供了特定的结构,Rails 可以在其助手中使用该结构。 redirect_to:

就是这种情况
          Prefix Verb     URI Pattern                  Controller#Action

        articles GET      /articles(.:format)          articles#index
                 POST     /articles(.:format)          articles#create
     new_article GET      /articles/new(.:format)      articles#new
    edit_article GET      /articles/:id/edit(.:format) articles#edit
         article GET      /articles/:id(.:format)      articles#show
                 PATCH    /articles/:id(.:format)      articles#update
                 PUT      /articles/:id(.:format)      articles#update
                 DELETE   /articles/:id(.:format)      articles#destroy

Rails 期望其对象符合特定结构,由 CRUD / RESTful routes:

支持

因此,如果你有@article(符合Rails的模型结构,并且你的路由设置如下):

#config/routes.rb
resources :articles

Rails 将能够获取 Article 对象的 实例 ,并使用它来填充 redirect_to 路由。

--

这就是 Rails 填充其许多辅助方法的方式,包括 form_forrender。这是基于管理框架的约定的智能猜测。

这就是为什么 Rails 程序员经常急于指出 "Railsy" 他们的代码如何。