简体   繁体   English

.each的结果,Ruby on Rails

[英]The result of .each in view, Ruby on Rails

I have some problem with my .each loop. 我的.each循环有问题。 The result of .each is displayed like json in my view: .each的结果在我看来像json一样显示:

一些描述 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| %>应该是<% @articles.each do |article| %> <% @articles.each do |article| %> don't print the iterator <% @articles.each do |article| %>不打印迭代器

remove the the '=' sign in <%= @articles.each do |article| %> 除去<%= @articles.each do |article| %>的'='符号。 <%= @articles.each do |article| %> --> <% @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| %> <%= @articles.each do |article| %> shouldn't have the equal sign. <%= @articles.each do |article| %>不应包含等号。

You are displaying the each loop in the erb view. 您正在erb视图中显示each循环。 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| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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