I am trying to render a partial on a page...like so
../devise/sessions/new.html.erb
<%= render partial: '/tweets/latest_tweets' %>
All good
In the partial I would like to do "each do" like so...
../tweets/_latest_tweets.html.erb
<% @tweets.each do |tweet| %>
<%= tweet.content %>
<% end %>
I get undefined method `each' for nil:NilClass
How do I define this method for my partial in my tweets controller?
I have index like so
tweets_controller.rb
def index
@tweets = Tweet.all
end
Which is fine for the index but I don't know how to do same for my partial (I tried just defining my partial like the index but that did not seem to work)
Anyone have a solution I could try?
ty
You need to pass the collection when rendering the partial. It'll give something like this:
devise/sessions/new.html.erb
<%= render partial: 'tweets/latest_tweets', collection: @tweets, as: :tweet %>
tweets/_latest_tweets.html.erb
<%= tweet.content %>
Just like that, and it will iterate through your collection automatically.
Fore more info, please read Rails - Layout and Rendering