samedi 21 novembre 2015

Why submitting an invalid post breaks a micropost feed (Railstutorial chapter 11)?

I'm about halfway through chapter 11 of Hartl's Railstutorial where it's showing how to add a micropost feed to a home page. It does this through the code:

@feed_items = current_user.feed.paginate(page: params[:page])

where feed is the method

  def feed
    Micropost.where("user_id = ?", id)
  end

Now in homepage where the micropost feed is suppose to be you have a partial containing:

<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

Now the tutorial mentions that on the homepage, if you submit an invalid micropost, it will break:

"on failed micropost submission, the Home page expects an @feed_items instance variable, so failed submissions currently break."

I don't understand the explanation as to why this breaks. Shouldn't @feed_items consist of all the other valid microposts from the database? So that even if you submit an invalid post, @feed_items gets populated with the previous valid microposts? I don't understand how an invalid micropost is able to affect @feed_items, especially since @feed_items draws microposts from the database, which only contains valid microposts because of validations present on microposts that are submitted.

Aucun commentaire:

Enregistrer un commentaire