Re: navigation list order by

33 views
Skip to first unread message

Rick

unread,
Mar 4, 2014, 8:04:51 AM3/4/14
to rubyonra...@googlegroups.com
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.


On Tuesday, March 4, 2014 5:25:54 AM UTC-5, Steven Cahill wrote:
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

Donald Ziesig

unread,
Mar 4, 2014, 12:14:45 PM3/4/14
to rubyonra...@googlegroups.com
Hi Steve:

Try the following:

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

rather than Category.all ...

See http://guides.rubyonrails.org/active_record_querying.html#ordering

Don Ziesig

On 03/04/2014 12:04 PM, Steven Cahill wrote:
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 

<li class="link"><% Category.all.each do |category| %>
  <%= link_to category.name, category_path(category), :order => ('id desc') %>
<% end %></li>
        </ul>
      </div>
    </nav> 
it just displays the list in a random order.


On Tuesday, March 4, 2014 10:25:54 AM UTC, Steven Cahill wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5dce3073-c36a-4b12-a0d2-4a1e2b155835%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Scott Ribe

unread,
Mar 4, 2014, 12:16:57 PM3/4/14
to rubyonra...@googlegroups.com, Steven Cahill
On Mar 4, 2014, at 10:04 AM, Steven Cahill <ste...@yorkshirebroadband.com> 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 Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Rick

unread,
Mar 4, 2014, 6:16:06 PM3/4/14
to rubyonra...@googlegroups.com


On Tuesday, March 4, 2014 12:04:46 PM UTC-5, Steven Cahill wrote:
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 

 
If, by "application layout page" you mean app/views/catagories/index.html.erb, use @categories.each not Category.all.each.



Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages