This section shows how to make a list of items in Ruby on Rails, so that you can edit, delete, preview each item from the list. This view will also serve as the point where one can create a new Page, or whatever model you created in your project, Person, Game, Project, User, etc.
1 go into your controller file, for this blog I have been using Pages as the controller name: app/controllers/admin/pages_controller.rb
2 find the “def index”, or create it with the following code:
def index
#use the first line for the public query
#@pages = Page.find(:all,:conditions=> {:showpage_flag => true} , :order => “page_order DESC”)
@pages = Page.find(:all,:conditions=> {:order => “page_order DESC”)
end
3 open or create this file: /app/views/admin/pages/index.html.erb
add the following code for a basic loop:
<%= link_to “New Page”, new_admin_page_path() %> <br>
<% @pages.each do |page| %>
<%=link_to “Edit”, edit_admin_page_path(page) %>
<%=link_to “Delete”, admin_page_path(page), :confirm => “Are you sure you want to delete ” + page.title + “?”, :method=>:delete %>
<%=link_to “Preview”, admin_page_path(page) %>
<%= page.title %> <br>
<% end %>
Thats all, run http://localhost:3000/admin/pages/
you should see a “New Page” link
