Navigation list order by

Hello Everybody

I have a navigation that draws data from a category table, I would like
to
display the navigation by category ID order, this is the code I have so
far

<%= link_to category.title, category_path(category) %> in the view

not sure where to go next to get order by category id

Steven

Not sure from your question, you seem to be asking “How do I sort the
Categories index?” but you’ve provided a link_to that deals with a
single
Category. Typically, you’ld order your Categories in the controller
index
method and the ordered list would be available in the index.html.erb
view
as @categories. You could then step through this list in your view with
@catagories.each do |category|.

If that’s what you’ve done then “category” (in your link_to) should be
the
id of a single category.

At any rate, you would really help yourself by getting familiar with the
Rails Guides at http:guides.rubyonrails.org.

Thanks Rick

on the actual categories page the categories are listed in category_id
order correctly, I have the following code in my controller

GET /categories

GET /categories.json

def index
@categories = Category.order(‘id asc’)
end

The only problem is the navigation list that I have put into the
application layout page seems to have a mind of its own, here is the
code
for

  • <% Category.all.each do |category| %> <%= link_to category.name, category_path(category), :order => ('id desc') %> <% end %>
  • it just displays the list in a random order.

    Hi Steve:

    Try the following:

    <% Category.order("id DESC).each do |category| %>

    rather than Category.all …

    See Active Record Query Interface — Ruby on Rails Guides

    Don Z.

    On Mar 4, 2014, at 10:04 AM, Steven C.
    [email protected] wrote:

    def index
    @categories = Category.order(‘id asc’)
    end

    The only problem is the navigation list that I have put into the application
    layout page seems to have a mind of its own, here is the code for

    So why are you using Category.all instead of @categories?


    Scott R.
    [email protected]
    http://www.elevated-dev.com/
    (303) 722-0567 voice

    On Tuesday, March 4, 2014 12:04:46 PM UTC-5, Steven C. wrote:

    end

    The only problem is the navigation list that I have put into the
    application layout page seems to have a mind of its own, here is the code
    for

    If, by “application layout page” you mean
    app/views/catagories/index.html.erb, use @categories.each
    notCategory.all.each.