Topic: link_to function? new view from function?

Hey Guys,

I have created a function that collects all the entries of a model (called Nismo) that begins with a selected letter/number:

    def showByLetter(letter)
        @nismosByLetter = Nismo.all :conditions => ['substr(name,1,1) = ?', letter]
    end

I have this inside my "Nismo" controller.

In my index view of the Nismo controller I have an alphabetical menu that I want to use to allow the user to select, for example, "B". This will then run the showByLetter function and then show in a page all the entries that start with that letter.

My problem is that in my index page I really don't know how to get this all to happen:
   
    <% Array('A' .. 'Z').each do |letter| %>
        <%= link_to letter, showByLetter(letter) %>
    <% end %>


I am guessing I need to create a new view.html.erb file for this "showByLetter" function to display it's results in.

I have tried to get my head around it from the programatic programmers book and have seen comments on the net that might mean I need to play with the routes.rb file to add something like to get the route right:

    namespace "nismo" do
        resources :nismos do
            member do
                get :showByLetter
            end
        end
    end

I am sure this is all very simple but I am confused and need some pointers.

any help would be appreciated!

Thanks

Adam

Re: link_to function? new view from function?

First things first, lets make sure I am clear and that should make things simpler. The goal is to allow a user to select only those entries in the db that start with a certain letter and then get an index of only those entries, correct?

if so, a few notes...

1. forget the idea of pulling showbyletter out of the normal index view. You truely have one action here, index. Get rid of the member do..., and any views named showbyletter.

2. In the index view you need a series of links something like what you had but ...

<% Array('A' .. 'Z').each do |letter| %>
        <%= link_to letter, "/nismos/"<<letter %> //you'll need to work this part out based on routes
 <% end %>

3. Then adj. your routes to pick up  the paramater

match /nismos/*l => 'nismos#index'

4. in the index controller you would then test for the existence of the 'l' parameter

if (params[:l])
  @nismos = Nismo.all :conditions => ['substr(name,1,1) = ?', letter]
else
  @nismos = Nismo.all 
end

5. Finally your index view stays the same, echoing out the value of nismos.

Hope that helps guide you in a simpler direction. Always remember, if you are adding more than your RESTful actions, you are probably making it much harder on yourself.

Re: link_to function? new view from function?

acooper86 thank you very much! It now works, and is much easier for me to understand!
Thanks a million!



acooper86 wrote:

First things first, lets make sure I am clear and that should make things simpler. The goal is to allow a user to select only those entries in the db that start with a certain letter and then get an index of only those entries, correct?

if so, a few notes...

1. forget the idea of pulling showbyletter out of the normal index view. You truely have one action here, index. Get rid of the member do..., and any views named showbyletter.

2. In the index view you need a series of links something like what you had but ...

<% Array('A' .. 'Z').each do |letter| %>
        <%= link_to letter, "/nismos/"<<letter %> //you'll need to work this part out based on routes
 <% end %>

3. Then adj. your routes to pick up  the paramater

match /nismos/*l => 'nismos#index'

4. in the index controller you would then test for the existence of the 'l' parameter

if (params[:l])
  @nismos = Nismo.all :conditions => ['substr(name,1,1) = ?', letter]
else
  @nismos = Nismo.all 
end

5. Finally your index view stays the same, echoing out the value of nismos.

Hope that helps guide you in a simpler direction. Always remember, if you are adding more than your RESTful actions, you are probably making it much harder on yourself.