【发布时间】:2020-05-09 23:37:33
【问题描述】:
我目前正在学习 Rails 课程,并在索引页面上显示存储在数据库中的所有电影。理想情况下,我希望将其限制为仅显示 5 部电影,并在下方有一个导航按钮,允许您在必要时显示更多电影。这是我的一些电影代码。
index.html.erb
<% @movies.each do |movie| %>
<li>
<article class="movie">
<header>
<%= image_for(movie) %>
<h2><%= link_to movie.title, movie %> (<%= movie.released_on.year %>)</h2>
<h3><%= movie.cast %></h3>
</header>
<p>
<%= truncate(movie.description, length: 150, separator: ' ') %>
</p>
<table>
<tr>
<th>Rating</th>
<td><%= movie.rating %></td>
</tr>
<tr>
<th>Duration</th>
<td><%= movie.duration %></td>
</tr>
<tr>
<th>Total Gross</th>
<td><%= format_total_gross(movie) %></td>
</tr>
</table>
<footer>
</footer>
</article>
</li>
<% end %>
<%= button_to 'View more', root_path, params: { state: :submitted }%>
</ul>
控制器
def index
@movies = Movie.all
end
【问题讨论】:
标签: ruby-on-rails ruby sqlite limit truncate