How to show blog post in alphabetical order.
Thank you.
PS: New to rails and web.
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 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.
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’
end
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’;
end
–
view:
<% 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 ] ] )
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
raj wrote:
Thank you.
Don’t call showa. call show and pass the letter as a param.
Thank you that worked.
right…
change to:
title LIKE ?
and
append the % on the end of the params[ :starts_with ]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs