How does it go to the show action

I'm new to ruby ​​on rails, and I want to understand how this piece of code redirects to the action of the show thanks to:

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

  redirect_to @article
end
+4
source share
3 answers

If you define routes using the Rails convention to define RESTful routes, i.e. resources :articlesin your file routes.rb, then it redirect_to @articlewill lead you to the page of showthis particular instance @article. Rails does the magic here.

When you write resources :articlesin your file routes.rb, Rails automatically creates these routes:

      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 controller show action:

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

, : redirect_to @article, show @article.

, RESTful, . Rails

+3

:

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,
+2

, , Ruby, Rails.


Rails Ruby, , , - , :

enter image description here

Rails , , Ruby .

- , Ruby classes, . methods, .

Rails ( Ruby) :

  • (def self.x)
  • (def x)

. , , @alien.is_visible?

, Ruby .


Rails Ruby (Ruby - , Rails - ), , Rails .

, , Rails MVC ( M odel V iew C ontroller):

enter image description here

, Rails . @article = Article.find x.

, , redirect_to @article, , Rails (IE - 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:

enter image description here

, @article ( Rails, , ):

#config/routes.rb
resources :articles

enter image description here

Rails Article redirect_to.

-

Here's how Rails populates many of its helper methods, including form_forand render. This is intellectual guessing based on conventions that define the structure.

This is why Rails programmers often seek to indicate how their "Railsy" code is.

+1
source

Source: https://habr.com/ru/post/1609061/


All Articles