Display by alphabetical letter

Hey all, so I have a table of games in my rails application. The table
has many of records within in.

What I wish to do is have at the top of the page a sort of link system
like the following

A | B | C | D | … | X | Y | Z |

What I want to do is when you click on a letter it will then display
each of the games by their alphabetical record.

It will do this by searching the game_name column but only extracting
those with the first letter not that of which it includes the letter
anywhere in the name.

How would I go about doing this. I have done search systems before but
can only do one search for a page based on a column. I want it to be a
search in which any letter can be clicked and then will link you to
associated games.

I am a new user of rails so any support would be of help.

Thanks
Christopher J.

On 19 February 2012 19:48, Christopher J. [email protected]
wrote:

It will do this by searching the game_name column but only extracting
those with the first letter not that of which it includes the letter
anywhere in the name.

How would I go about doing this. I have done search systems before but
can only do one search for a page based on a column. I want it to be a
search in which any letter can be clicked and then will link you to
associated games.

Assuming it is the find itself that is the problem try something like
Games.where( “name LIKE ?”, “#{params[:letter]}%” )

You can play with this in the rails console to get the syntax right.
Obviously in that case you would not use params, but just a letter.

Colin

Colin L. wrote in post #1047699:

On 19 February 2012 19:48, Christopher J. [email protected]
wrote:

It will do this by searching the game_name column but only extracting
those with the first letter not that of which it includes the letter
anywhere in the name.

How would I go about doing this. I have done search systems before but
can only do one search for a page based on a column. I want it to be a
search in which any letter can be clicked and then will link you to
associated games.

Assuming it is the find itself that is the problem try something like
Games.where( “name LIKE ?”, “#{params[:letter]}%” )

You can play with this in the rails console to get the syntax right.
Obviously in that case you would not use params, but just a letter.

Colin

So I have in my index.html.erb the following

link_to 'A'

|

link_to 'B'

|

link_to 'C'

|

link_to 'D'

|

link_to 'E'

|

link_to 'F'

| ......

link_to 'Z'

How would I go about writting the direction of each of these to connect
to as you said: Game.where( “name LIKE ?”, “#{params[:letter]}%” )?

I am assuming that goes in the game.rb file like my other search is but
am not 100% sure on how to connect each letter to each part, do I read
in the quoted letter to the params? so it looks at the letter and
returns it to the search to get all related results?

Sorry if this is silly, just trying to learn.

Thanks
Chris J.

On 19 February 2012 20:48, Christopher J. [email protected]
wrote:

search in which any letter can be clicked and then will link you to
So I have in my index.html.erb the following
How would I go about writting the direction of each of these to connect
to as you said: Game.where( “name LIKE ?”, “#{params[:letter]}%” )?

I am assuming that goes in the game.rb file like my other search is but
am not 100% sure on how to connect each letter to each part, do I read
in the quoted letter to the params? so it looks at the letter and
returns it to the search to get all related results?

Something like
<%= link_to ‘A’, games_path(:letter => ‘A’) %>

will request games_controller#index with params[:letter] ‘A’, then in
index you can find just the appropriate records. Of course you would
actually do the links in a loop not individually with ‘A’, ‘B’ etc.

Sorry if this is silly, just trying to learn.

Not silly, just lack of knowledge. Have you worked through some
tutorials? railstutorial.org is good and is free to use online. Work
right through it even though you may think the app it is developing is
of no interest to you, you will learn the basic techniques to allow
you to develop your own app.

Colin

On 20 February 2012 02:18, Christopher J. [email protected]
wrote:

I shall check out those links, lately I have been listening to the casts
on railscasts.org and they have proved very useful.

I think you would be better to work right through railstutorial before
going further so that you will get a better grasp of the basics.

Do you know of any good tutorials of using links to display associated
records of a database?

I tried the following but it does not work:

<%= link_to ‘f’, games_path(:game_name => ‘f’) %>

It runs but just returns the full table. I even tried putting in a full
record in the brackets such as the following:

What do you see in development.log when you click the link - you
should be able to see the name letter being passed in to the GET
request.

Show us that bit of the log and the code you have written for the
action to interpret the value passed in params.

Colin

Started GET “/games?game_name=F” for 127.0.0.1 at 2012-02-20 11:53:51
+0000
Creating scope :page. Overwriting existing method Game.page.
Processing by GamesController#index as HTML
Parameters: {“game_name”=>“Fifa 12”}
[1m[36mGame Load (127.0ms)[0m [1mSELECT games.* FROM games LIMIT
4 OFFSET 0[0m
Rendered games/index.html.erb within layouts/application (333.0ms)
Completed 200 OK in 425ms (Views: 137.0ms | ActiveRecord: 287.0ms |
Sphinx: 0.0ms)

That is what is in my log when I click the F button of the alphabet.

Thanks
Chris J.

I shall check out those links, lately I have been listening to the casts
on railscasts.org and they have proved very useful.

Do you know of any good tutorials of using links to display associated
records of a database?

I tried the following but it does not work:

<%= link_to ‘f’, games_path(:game_name => ‘f’) %>

It runs but just returns the full table. I even tried putting in a full
record in the brackets such as the following:

<%= link_to ‘f’, games_path(:game_name => ‘Fifa 12’) %>

hoping that the result would return that record to see if it works but
no luck.

Thanks
Chris J.

On 20 February 2012 11:57, Christopher J. [email protected]
wrote:

That is what is in my log when I click the F button of the alphabet.

And what code have you written in the index action to interpret the
letter from the url? If nothing then /please/ work through the
tutorial I suggested so that you will have some idea of what rails is
all about.

Colin

To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

Ok so I have been playing around with this search for the past few hours
and have done the following but still no result.

I have the following in my index.html.erb:

<% for char in ‘A’…‘Z’ %>
<%= link_to( “#{char}”,
:update => “content”,
:char => char,
:url =>{:action => :live_search }) %>
<% end %>

And then I have the following in my games_controller.rb:

def live_search
puts ‘live_search’
@game = Game.find(:all,:conditions => [“lower(game_name)like
?”,"%" + params[:search].downcase+ “%”])
puts @game
if params[‘search’].to_s.size < 1
render :nothing => true
else
if @game.size > 0
render :partial => ‘display’, :collection => @game
else
render :text => “

  • No results found
  • ”, :layout
    => false
    end
    end
    end

    Thanks for all the help so far.
    Chris J.

    On Mon, Feb 20, 2012 at 2:09 PM, Christopher J.
    [email protected]wrote:

    <% end %>
    else
    Chris J.

    I haven’t read the entire thread (just some parts)

    Maybe this is to basic, but it helped me

    and maybe this to (because as far as I understand you’re trying to get
    some
    pagination… or maybe I’m wrong

    The approach on the first one will help you with this

    @game = Game.find(:all,:conditions => ["lower(game_name) like ?","%" 
    

    params[:search].downcase+ “%”])

    Javier Q.

    Javier Q. wrote in post #1047871:

    On Mon, Feb 20, 2012 at 2:09 PM, Christopher J.
    [email protected]wrote:

    <% end %>
    else
    Chris J.

    I haven’t read the entire thread (just some parts)

    Maybe this is to basic, but it helped me
    #111 Advanced Search Form - RailsCasts

    and maybe this to (because as far as I understand you’re trying to get
    some
    pagination… or maybe I’m wrong

    #240 Search, Sort, Paginate with AJAX - RailsCasts

    The approach on the first one will help you with this

    @game = Game.find(:all,:conditions => ["lower(game_name) like ?","%"
    

    params[:search].downcase+ “%”])

    Javier Q.

    Hey Javier,

    I completed those two episodes. The first one I already use in the side
    bar for searching specific games and I have fully implemented the second
    episode (240) in order to include the column ordering and pagination.

    But am still troubled in actually displaying those games that start with
    the correct character. What I am trying to do is when you click on a
    letter of the alphabet in the top bar it displays all records that start
    with e.g. A.

    Thanks
    Chris J.

    On 20 February 2012 19:36, Christopher J. [email protected]
    wrote:

    I completed those two episodes. The first one I already use in the side
    bar for searching specific games and I have fully implemented the second
    episode (240) in order to include the column ordering and pagination.

    But am still troubled in actually displaying those games that start with
    the correct character.

    Have a look at the Rails Guide on Debugging for various debugging
    techniques to help you work out why the code is not working.

    Colin

    On Feb 20, 2012, at 12:36 PM, Christopher J. wrote:

    But am still troubled in actually displaying those games that start with
    the correct character.

    But you’re searching on a condition for games that contain the
    character, instead of games that start with the character.

    Colin L. wrote in post #1047882:

    On 20 February 2012 19:36, Christopher J. [email protected]
    wrote:

    I completed those two episodes. The first one I already use in the side
    bar for searching specific games and I have fully implemented the second
    episode (240) in order to include the column ordering and pagination.

    But am still troubled in actually displaying those games that start with
    the correct character.

    Have a look at the Rails Guide on Debugging for various debugging
    techniques to help you work out why the code is not working.

    Colin

    Hey Colin,

    I provided my code above, does it all seem ok to you or are there any
    noticeable errors in the code? I shall have a look at debugging the
    problem.

    Thanks
    Chris

    On 20 February 2012 21:43, Christopher J. [email protected]
    wrote:

    Any ideas of what my problem might be?

    Have a look at the Rails Guide on Debugging for various debugging
    techniques to help you work out why the code is not working.

    Colin

    Colin L. wrote in post #1047891:

    On 20 February 2012 21:43, Christopher J. [email protected]
    wrote:

    Any ideas of what my problem might be?

    Have a look at the Rails Guide on Debugging for various debugging
    techniques to help you work out why the code is not working.

    Colin

    Ok Colin,

    From what I have understood it isn’t actually connecting with the
    controller method, simply passing the params in the view text.

    This is what I have in my log:

    Started GET “/games?char=F&update=content&url%5Baction%5D=live_search”
    for 127.0.0.1 at 2012-02-20 22:14:02 +0000
    Creating scope :page. Overwriting existing method Game.page.
    Processing by GamesController#index as HTML
    Parameters: {“char”=>“F”, “update”=>“content”,
    “url”=>{“action”=>“live_search”}}
    [1m[36mGame Load (1.0ms)[0m [1mSELECT games.* FROM games LIMIT 4
    OFFSET 0[0m
    [1m[35m (1.0ms)[0m SELECT COUNT() FROM games
    Rendered games/index.html.erb within layouts/application (78.0ms)
    Creating scope :page. Overwriting existing method User.page.
    [1m[36mUser Load (1.0ms)[0m [1mSELECT users.
    FROM users WHERE
    users.id = ? LIMIT 1[0m [[“id”, 9]]
    Completed 200 OK in 392ms (Views: 360.0ms | ActiveRecord: 31.0ms |
    Sphinx: 0.0ms)

    As you can see it has the parameters and then just moves on but I am not
    sure why.

    Thanks
    Chris J…

    But you’re searching on a condition for games that contain the
    character, instead of games that start with the character.

    What would I change to display by starting character?

    Also it does not seem to be searching it regardless because if for
    example I click the character X (a character that none of the records at
    the moment contains) it still returns them all and if I click the letter
    F (a character only one of the records contains) it still returns them
    all.

    Any ideas of what my problem might be?

    On Feb 20, 2012, at 2:43 PM, Christopher J. wrote:

    What would I change to display by starting character?

    Hint: look at the wildcards in the query you are building…


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