GrégoireC
GrégoireC

Reputation: 279

Articles appear too many times

I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.

Here's my AccueilController's code :

def index

@postsTest = Post.last(1)
@articles = Article.last(1)
@articlesList = Article.last(2)

    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
end

Here's my application.html.erb code :

<div id="container">
    <div id="col1">
        <%= render :partial => 'shared/listArticles', :collection => @articlesList %>
        <div class="clear"></div>
    </div>
    <div id="col2">
        <%= yield %>
    </div>
    </div>
</div>

And now my shared/listArticles's code :

<% @articlesList.each do |article| %>
        <div id="blocArticle">
            <div id="pic">
                <%= image_tag (article.photo.url(:thumb)) %>
            </div>
            <div id="contentArticle">
                <div id="titleArticle">
                    <%= link_to article.title, article %>
                    <div class="clear"></div>
                </div>
                <div id="contentArticleDesc">

                        <%= link_to article.desc, article %>

                    <div class="clear"></div>
                </div>

                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
    <% end %>   

And my problem is that if I write @articlesList = Article.last(2) then the last two articles will appear two times; if I write @articlesList = Article.last(3) then the last three articles will appear three times etc...

Of course, I would like that each of them appear just one time.

Does someone have any ideas about the source of the problem ?

Upvotes: 2

Views: 78

Answers (1)

Anthony Alberto
Anthony Alberto

Reputation: 10395

You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!

Upvotes: 1

Related Questions