•  

    Now that there are validations, calling @article.save on an invalid article will return false. If you open app/controllers/articles_controller.rb again, you'll see that we don't check the result of calling @article.save in the create action. If @article.save fails in this situation, we need to show the form to the user again. To do this, replace the new and create actions in app/controllers/articles_controller.rb with these:

     

    def new

      @article = article.new

    end

     

    def create

      @article = Article.new(article_params)

     

      if @article.save

        redirect_to @article

      else

        render 'new'

      end

    end

     

    private

      def article_params

        params.require(:article).permit(:title, :text)

      end

    Now the new action creates a new instance variable called @article, and you'll see why in a couple of paragraphs.

     

    Note that in the create action we used render instead of redirect_to when save returns false. The render method is used to have the @article object passed back to the new template when it is rendered. This rendering is done within the same request as the form submission, while redirect_to tells the browser to make another request.

     

    If you reload localhost:3000/articles/new and try to save an article without a title, Rails will take you back to the form, but that's not very helpful. You need to tell the user that something went wrong. To do this, modify app/views/articles/new.html.erb to check for error messages:


    votre commentaire
  •  

     

    If you want to refer to an action on the same controller, you don't need to specify the :controller option because Rails uses the current controller by default.

     

    TIP: In development mode (which you use by default), Rails reloads your application with every browser request, so you don't have to stop and restart your web server when you make changes.

     

    Let's add some validations

    The model file app/models/article.rb is as simple as it looks:

     

    class Article < ApplicationRecord

    end

    Not much is written in this file, but note that the Article class is derived from ApplicationRecord. ApplicationRecord inherits from ActiveRecord::Base which provides a lot of functionality for your Rails models, including basic database operations CRUD (Create, Read, Update, Destroy), data validation, sophisticated search support, and the ability to establish relationships between different models.

     

    Rails includes methods to help validate the data you pass into the model. Open the app/models/article.rb file and edit:

     

    class Article < ApplicationRecord

      validates :title, presence: true,

                        length: { minimum: 5 }

    end

    These changes will ensure that all articles have a title that is at least five characters long. Rails can check for various conditions in the model, including the existence or uniqueness of fields, their format, and the existence of related objects. More validations are covered in Active Record Validation.


    votre commentaire



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