# Alphabetical view of posts

**URL:** <https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733>\
**Category:** Rails\
**Created:** [May 21, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733 "2007-05-21T23:19:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 21, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/1 "2007-05-21T23:19:28Z")

</div>

How to show blog post in alphabetical order.

Thank you.

PS: New to rails and web.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 22, 2007, 6:00am UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/2 "2007-05-22T06:00:53Z")

</div>

On May 21, 4:19 pm, raj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to show blog post in alphabetical order.
> 
> Thank you.
> 
> PS: New to rails and web.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

You would sort them in the controller. Your controller may look like:

class PostsController \< ApplicationController  
def index  
@posts = Post.find(:all)  
…  
end  
…  
end

Change the find to be:

```
@posts = Post.find(:all, :order => "last_name, first_name")

```

So, @posts, when it gets to the view, is ordered the way you want it.  
The view simply does the viewing.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 22, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/3 "2007-05-22T20:50:19Z")

</div>

I have a small problem here…

like when they click on A i am calling showa function in controller,  
where I get all the post whose title starts with A. This I am doing for  
all the alphabets. But I am trying to use only one view and redirecting  
to list…but it says @posts is undefined in the view.

On solution is to have one view page for each alphabet but i need to  
have 24 pages which is a wrong design. How do I handle this.

Thank you.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 22, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/4 "2007-05-22T22:11:55Z")

</div>

Can you be more precise, i am new bee…

this my show method  
def showa  
@posts=Post.find(:all, :conditions =\> “title LIKE ‘a%’”, :order=\>  
“id DESC”)  
render :action =\> ‘list’  
end

This is my view stmt

\<%= link\_to ‘A’, :action =\> ‘showa’ %\>

Thank You.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 23, 2007, 12:31am UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/5 "2007-05-23T00:31:42Z")

</div>

def show  
@posts = Post.find( :all, :conditions =\> [ “title LIKE ‘?%’”, params[  
:starts\_with ] ] )  
render :action =\> ‘list’;  
end

–

view:

\<% for alpha in (‘A’…‘Z’) -%\>  
\<%= link\_to alpha, { :action=\> ‘show’, :starts\_with =\> alpha }  
\<% end -%\>

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 23, 2007, 5:47am UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/6 "2007-05-23T05:47:14Z")

</div>

My new list is

def list  
@posts = Post.find( :all, :conditions =\> [ “title LIKE ‘?%’”, params[  
:starts\_with ] ] )

end

I am getting Mysql error ☹

Mysql::Error: #42000You have an error in your SQL syntax; check the  
manual that corresponds to your MySQL server version for the right  
syntax to use near ‘T’%’)’ at line 1: SELECT \* FROM posts WHERE (title  
LIKE ‘‘T’%’)

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/connection\_adapters/abstract\_adapter.rb:128:in  
`log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/mysql_adapter.rb:243:in`execute’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/connection\_adapters/mysql\_adapter.rb:399:in  
`select' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in`select\_all’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:427:in  
`find_by_sql' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:997:in`find\_every’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active\_record/base.rb:418:in  
`find’  
#{RAILS\_ROOT}/app/controllers/blog\_controller.rb

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 22, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/7 "2007-05-22T20:56:20Z")

</div>

raj wrote:

> Thank you.

Don’t call showa. call show and pass the letter as a param.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 23, 2007, 7:19am UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/8 "2007-05-23T07:19:45Z")

</div>

Thank you that worked.

---

<div class="post-metadata">

**Author:** ![raj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c0e974/32.png) [@raj](https://www.ruby-forum.com/u/raj)\
**Post date:** [May 23, 2007, 6:05am UTC](https://www.ruby-forum.com/t/alphabetical-view-of-posts/104733/9 "2007-05-23T06:05:04Z")

</div>

right…

change to:

title LIKE ?

and

append the % on the end of the params[:starts\_with]
