Alphabetical view of posts

How to show blog post in alphabetical order.

Thank you.

PS: New to rails and web.

On May 21, 4:19 pm, raj [email protected] wrote:

How to show blog post in alphabetical order.

Thank you.

PS: New to rails and web.

Posted via

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

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



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.

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.

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’

This is my view stmt

<%= link_to ‘A’, :action => ‘showa’ %>

Thank You.

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


<% for alpha in (‘A’…‘Z’) -%>
<%= link_to alpha, { :action=> ‘show’, :starts_with => alpha }
<% end -%>

My new list is

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


I am getting Mysql error :frowning:

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

log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/mysql_adapter.rb:243:inexecute’
select' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/connection_adapters/abstract/database_statements.rb:7:inselect_all’
find_by_sql' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:997:infind_every’

raj wrote:

Thank you.

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

Thank you that worked.


change to:

title LIKE ?


append the % on the end of the params[ :starts_with ]