• Ruby on Rails Blog Article Update

     

     

    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.


    Tags Tags : ,
  • Commentaires

    Aucun commentaire pour le moment

    Suivre le flux RSS des commentaires


    Ajouter un commentaire

    Nom / Pseudo :

    E-mail (facultatif) :

    Site Web (facultatif) :

    Commentaire :