Reputation: 53
In my Rails Blog app I have my welcome controller and view index.html.erb
. And I have my articles controller and views index.html.erb
and show.html.erb
.
My articles view has two views, one for listing articles index.html.erb
, and the other for showing articles show.html.erb
. When listing articles in my articles view everything works perfectly. But my goal is to list the most recent article in a popup modal in my welcome view index.html.erb
.
Below is the code for listing articles in the articles view
<% @articles.reverse.each do |article| %>
<div class="w3-quarter">
<div class="panel panel-default" style="width: 300px; min-height: 300px;">
<div class="panel-heading">
<h3><%= article.title %></h3>
<p class="w3-opacity"><%= article.created_at.strftime("%b %d, %Y") %> | Comments <span class="label label-primary"><%= article.comments.count %></span> </p>
</div>
<div class="panel-body">
<p><%= article.text.first(250) %> ...</p>
<%= link_to 'Read More', article_path(article), class: 'btn btn-primary btn-block' %>
</div>
</div>
</div>
<% end %>
And below is the code for listing the most recent article in my welcome view
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<h2><%= @article.title %></h2>
<p class="w3-opacity">Posted on <%= @article.created_at.strftime("%b %d, %Y") %></p>
<hr>
<p><%= @article.text.first(500) %></p>
</div>
</div>
</div>
</div>
I get the following error from my welcome index.html.erb
:
undefined method `title' for nil:NilClass
Any ideas?
Upvotes: 1
Views: 205
Reputation: 3175
Probably you are not setting @article
in you welcome_controller
Try something this
class WeclomeController < ApplicationController
def index
@artice = Article.order(created_at: :desc).first
end
end
Upvotes: 1