简体   繁体   中英

The result of .each in view, Ruby on Rails

I have some problem with my .each loop. The result of .each is displayed like json in my view:

一些描述 Here is a code from my controller:

def index
  @articles = Article.all
end

And here is my view:

<h1>Всі статті</h1>
<%= link_to "Нова стаття", new_article_path %>
<table>
    <tr>
        <th>Заголовок</th>
        <th>Вміст</th>
        <th colspan="2"></th>
    </tr>

    <%= @articles.each do |article| %>
        <tr>
            <td><%= article.title %></td>
            <td><%= article.text %></td>
            <td><%= link_to "Переглянути", article_path(article) %></td>
            <td><%= link_to "Редагувати", edit_article_path(article) %></td>
        </tr>
    <% end %>
</table>

What is the problem?

Loops return their array. The <%= is outputting the return of the loop. Remove the =.

Renders array:

 <%= @articles.each do |article| %>

Does not render array:

 <% @articles.each do |article| %>

<%= @articles.each do |article| %> <%= @articles.each do |article| %> should be <% @articles.each do |article| %> <% @articles.each do |article| %> don't print the iterator

remove the the '=' sign in <%= @articles.each do |article| %> <%= @articles.each do |article| %> --> <% @articles.each do |article| %> <% @articles.each do |article| %>

Pretty sure I've run into this same problem a number of times.

This line right here <%= @articles.each do |article| %> <%= @articles.each do |article| %> shouldn't have the equal sign.

You are displaying the each loop in the erb view. Each loops should not have the = sign, or you will display the entire collection.

So turn this:

<%= @articles.each do |article| %>

Into this:

<% @articles.each do |article| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM