• Let's go back to our controller in app/controllers/articles_controller.rb and change the index action to retrieve all the articles from the database:

     

    class ArticlesController < ApplicationController

       def index

         @articles = Article.all

       end

    end

    Controller instance variables can be accessed from the view. This means we can link to @articles in app/views/articles/index.html.erb. Let's open this file and replace its contents with:

     

    The above code is a mixture of HTML and ERB. ERB is a templating system that evaluates Ruby code embedded in a document. Here we see two types of ERB tags: <% %> and <%= %>. The <% %> tag means "evaluate enclosed Ruby code". The <%= %> tag means "evaluate the enclosed Ruby code and output the value it returns". Anything that can be written in a normal Ruby program can be nested within these ERB tags, although it's usually best to keep the contents of ERB tags concise for readability.


  • It's important to note that we've only initialized this object. This object is not stored in the database at all. It is currently only available in the console. To save it to the database, you need to call save:

     

    irb> article.save

    (0.1ms) begin transaction

    Article Create (0.4ms) INSERT INTO "articles" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?)

     

    The above output shows an INSERT INTO "articles" ... database query. This shows that an article has been inserted into our table. And if we look at the article object again, we can see that something interesting has happened:

     

    irb> article

    => #<Article id: 1, title: "Hello Rails", body: "I am on Rails!", created_at: "2020-01-18 23:47:30", updated_at: "2020-01-18 23 :47:30">


  • Now let's try to create a directory.

     

    We will use script/generate to generate the model, controller and views for the directory.

     

    <code>$>ruby script/generate scaffold_resource article title:string body_format:string body:text</code>

     

     

    I will not include the source code in the text, so as not to bloat the article, it can be viewed online, I will describe what different pieces of code are for.

     

    Ruby-on-Rails generated several files, let's look at some of them:

     

    app/models/article.rb - article model

    app/controllers/articles\_controller.rb - controller for managing the articles catalog

    config/routes.rb - added line map.resources :articles

    app/views/articles/... - views for creating, editing and viewing articles

    app/views/layouts/articles.rhtml - page template for working with the catalog

    db/migrate/001_create_articles.rb - create a table for articles in the database


  • Communication of objects and databases in ruby on rails is carried out using an OR mapper, which is called ActiveRecord. It deals with mapping fields from a database table to object fields, validating objects before saving, generating code to represent relationships between objects.

     

    To create a new model, it is enough to inherit from the ActiveRecord::Base class

     

    <code class='ruby' lang='ruby'>class Article < ActiveRecord::Base end</code>

     

     

    By default, ActiveRecord will work with a table named the same as the class, only in the plural. In our case Articles.

     

    All fields of the table can be accessed using the methods of the same name:

     

    <code class='ruby' lang='ruby'>#Let there be a title field in the Articles table

    Article.create(:title => 'Hello World!')

    article = Article.find(1)

    print article.title

    #=> Hello World!</code>






    Suivre le flux RSS des articles
    Suivre le flux RSS des commentaires