[英]The result of .each in view, Ruby on Rails
我的.each循環有問題。 .each的結果在我看來像json一樣顯示:
def index
@articles = Article.all
end
這是我的看法:
<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>
問題是什么?
循環返回其數組。 <%=正在輸出循環返回。 刪除=。
渲染數組:
<%= @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| %>
的'='符號。 <%= @articles.each do |article| %>
-> <% @articles.each do |article| %>
<% @articles.each do |article| %>
可以肯定的是,我已經多次遇到過同樣的問題。
這行就在這里<%= @articles.each do |article| %>
<%= @articles.each do |article| %>
不應包含等號。
您正在erb視圖中顯示each
循環。 每個循環都不應帶有=
號,否則您將顯示整個集合。
所以打開這個:
<%= @articles.each do |article| %>
變成這個:
<% @articles.each do |article| %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.