Разбиение без драгоценного камня Далее, Назад, кнопки для Name.order (: id) .limit (10) .offset (0)
Я искал решение моей проблемы уже более недели. У меня есть задание, на котором я потерял 10 баллов из-за отсутствия следующей / предыдущей функциональности, и у меня закончилось время. Я все еще хочу понять это, хотя.
Я создал короткий одностраничный сайт сrails generate scaffold Ripple name:string message:text url:string
который показывает индекс из 10 последних отображаемых сообщений (имя, сообщение, create_on, link_to «показать»). Мне все еще нужно создать следующую, предыдущую, самую новую, самую старую ссылку, чтобы показать следующие 10, предыдущие 10 .... результатов. Мой код
app\controllers\ripple_controller.rb
class RipplesController < ApplicationController
before_action :set_ripple, only: [:show, :update]
before_action :restrict_destroy_edit, only: [:edit, :destroy]
before_filter :set_page
helper_method :link_name_to_url, :next_button, :previous_button, :newest_button, :oldest_button, :is_next_page_available, :is_previous_page_available
RIPPLES_PER_PAGE = 10
def index
@ripples = Ripple.order(:id).limit(RIPPLES_PER_PAGE).offset(@page * RIPPLES_PER_PAGE)
end
#All my show, new, destroy, edit, create ....
def next_button
end
def previous_button
end
def newest_button
end
def oldest_button
end
def is_next_page_available?
end
def is_previous_page_available?
end
def set_page
@page = 5
end
private
...
\app\views\ripples.html.erb
<table>
<thead>
<tr>
<th>Name</th>
<th>Message</th>
<th>Posted</th>
<th>Show Ripple</th>
</tr>
</thead>
<tbody>
<% @ripples.each do |ripple| %>
<tr>
<td><%= link_name_to_url ripple %></td>
<td><%= truncate(ripple.message, length: 50) %></td>
<td><%= ripple.created_at.strftime("%B %d, %Y %l:%M %P") %></td>
<td><%= button_to 'Show', ripple, :method => "get" %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div id = "nav">
<button><%= link_to 'Newest' %></button>
<button><%= link_to 'Previous 10 Ripples' %></button>
<button><%= link_to "Next 10 Ripples" %></button>
<button><%= link_to 'Oldest' %></button>
<button><%= link_to 'New Ripple', new_ripple_path, class: "button", method: :get %></button>
</div>
И я пробовал вызывать методы в модели, но продолжаю получатьundefined method "next" for #<Class:0xb4eabd0c>
ошибка на следующем и предыдущем.
app\models\ripple.rb
class Ripple < ActiveRecord::Base
default_scope -> {order(created_at: :desc)}
validates :name, :message, presence: true
validates :url, allow_blank: true, format: {
with: URI::regexp(%w(http https)),
message: "Must be a url starting with http:// or https://"
}
def next
Ripple.order(:id).limit(10).offset((@page - 1) * 10)
end
def previous
Ripple.order(:id).limit(10).offset((@page + 1) * 10)
end
end
Как бы я реализовать следующий и предыдущий, используя order (). Limit (). Offset и, возможно, использовать@page
чтобы отслеживать, где я нахожусь в ActiveRecord. Может быть что-то вроде
def next_button
@page -= 1
end
что я могу позвонить в указатель"<%= link_to Next 10" next_button %>
В любом случае, у меня нет идей, которые могут сработать.
Спасибо за любую помощь.