• In contrast to machine-oriented languages that run faster, the goal of this development was the language closest to the human. Any work with a computer is done by people and for people, and it is necessary to take care first of all of the expended efforts of people. The language makes it as quick and simple as possible for a person to complete a task, although this may require additional computer time. The principles of programming and the structure of the language are sometimes singled out in the term Ruby Way: "simple, but not too simple", "the principle of least surprise", the secondary speed of the program, dynamism, simple strict rules, the implementation of which does not reach pedantry, the need to create useful and beautiful programs as the reason for programming.


  • Ruby also inherited the philosophy of the Perl programming language in terms of giving the programmer the ability to achieve the same result in several different ways. People are different, and for freedom they need the opportunity to choose. One of the main goals of the development was to free programmers from routine work that the calculator can perform faster and better. Particular attention, in particular, was paid to everyday routine activities (word processing, administration), and the language is especially well tuned for them.


  • Right now, we're pointing the form to an update action, which isn't defined yet, but we'll do that soon.

     

    Passing an article object to the method will automatically generate a url to submit the edited article form. This option tells Rails that we want this form to be submitted using PATCH, an HTTP method that is expected to be used to update resources according to the REST protocol.

     

    The form_with arguments can be model objects, such as model: @article , which will cause the helper to populate the form with the object's fields. Passing a symbol (scope: :article) to the namespace just creates the fields, but doesn't populate them with anything. Read more in the form_with documentation.

     

    Next, we need to create an update action in app/controllers/articles_controller.rb. Add it between the create action and the private method:

     

    def create

      @article = Article.new(article_params)

     

      if @article.save

        redirect_to @article

      else

        render 'new'

      end

    end


  •  

     

    A few things about what's going on. We check if there are any errors with @article.errors.any?, in which case we show a list of all errors with @article.errors.full_messages.

     

    pluralize is a rails helper that takes a number and a string as arguments. If the number is greater than one, the string will be automatically pluralized.

     

    The reason we added @article = Article.new to the ArticlesController is because otherwise @article would be nil in the view, and calling @article.errors.any? will cause an error.

     

    TIP: Rails automatically wraps errored fields in a div with class field_with_errors. You can define a css rule to make them stand out.

     

    Now we'll have a nice error message when saving an article without a title if we try this in the new article form http://localhost:3000/articles/new.

     

    Form with errors

     

     

    We have revealed the "CR" part of CRUD. Now let's focus on the "U" part, updating articles.

     

    The first step is to add an edit action to the ArticlesController, usually between the new and create actions, as shown.





    Suivre le flux RSS des articles de cette rubrique