0

I am relatively new to rails and using rails 3. I am trying to create an online glossary so that if a user clicks 'A' on the menu they will be shown entries from that database that all start with 'A'.

I know that you can do this using different pages such that you can have a 'letter_A.html.erb' file and 'letter_B.html.erb' file etc but I'm wondering if it can be done in the same file since I want to avoid repeating the same code over and over?

What I would like is if the user clicks on the link 'D' they may be taken to another page 'letter.html.erb' but only see the entries that begin with 'D'. And if they click 'A' they again get taken to the same page but only see entries starting with 'A'. I think you have to pass in a variable into the link_to function but I'm not sure how to do this.

Any help would be much appreciated.

Thanks in advance.

I have created a method inside my posts controller like so:

def showletter
@posts = Post.where(:letter => "B")
...
end

and that shows up all entries that start with the letter B for example. But what I am wondering is can you pass a variable into your controller so that the "B" can be replaced by a variable that will be between A..Z? From this I was hoping to use link_to_function or something similar in my view and call the same method for different links? I'm still new to rails so I'm not sure if this can be done - any help that anyone could offer would be great.

1 Answers1

1

Let's assume you have a route like this:

get '/glossary/:id', :as => :glossary

Now, in your index page you're going to have code that looks something like:

<% ('a'..'z').to_a.each do |f| %->
  <%= link_to "#{f}", "/glossary/#{f}" %>
<% end %>

or

<% ('a'..'z').to_a.each do |f| %->
  <%= link_to "#{f}", glossary_path(:id => f) %>
<% end %>

The second is preferable. Read more here. Keep in mind that you can experiment with your named routes in the rails console, like so:

$ rails console --sandbox

Loading development environment in sandbox (Rails 3.1.3)
Any modifications you make will be rolled back on exit
ruby-1.9.3-p0 :001 > app.glossary_path(:id => 'a')
 => "/glossary/a"
troutwine
  • 3,721
  • 3
  • 28
  • 62
  • Thanks for your quick response - I tried out your first option as the second one didn't work for me - I got an 'undefined method' error. I'm not sure if this is what I am looking for though - does this send you to another page labelled 'a'? – user1104150 Jan 17 '12 at 10:25
  • What I would like to do is by clicking the link 'a' you get taken to the glossary page but you only see words beginning with a. Thanks for your help :) – user1104150 Jan 17 '12 at 10:27
  • The second method is strongly dependent on how your routes are defined. Lookup 'named routes' for more information. What you're wanting isn't going to automagically happen; these links will redirect you to a controller and the controller will have to pull all pages starting with the 'id' letter. – troutwine Jan 17 '12 at 15:54
  • Yes you are right. I have edited my post to include a description of my controller method but I'm still not sure of a few things. Any help that you could offer would be great. – user1104150 Jan 18 '12 at 21:37
  • If you setup the routes as I advised, your call will be `:letter => params[:post][:id]`. – troutwine Jan 18 '12 at 23:19