# Controller, vista, passaggio variabile e routes.rb

**URL:** <https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855>\
**Category:** IT\
**Created:** [January 12, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855 "2008-01-12T21:59:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chiara79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chiara79](https://www.ruby-forum.com/u/chiara79)\
**Post date:** [January 12, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855/1 "2008-01-12T21:59:13Z")

</div>

Ciao, la mia webapp Ã¨ cosÃ¬ strutturata:  
DATABASE TABLE:  
products:id, name, category\_id(references categories(id),…  
categories:id, name;

APP/CONTROLLER:  
site\_controller.rb

def list  
@category\_pages, @category = paginate :category, :per\_page =\> 30  
end

def list\_cat  
@product\_pages,@category, @product = paginate :category, :per\_page  
=\> 30

end

APP/VIEW/SITE  
list.rhtml

```
    <table cellpadding="5" cellspacing="0">
    <%
    odd_or_even = 0
    for category in @category
      odd_or_even = 1 - odd_or_even
      cat = category.name
    %>
      <tr valign="top" <%= odd_or_even %>">

        <td>
           <%= link_to cat, :action => 'list_cat', :id => category

```

%\>  
  
  
  
\<% end %\>

list\_cat.rhtml

# Qui vorrei creare la lista dei prodotti presenti in una determinata categoria...

In pratica quello che vorrei Ã¨ creare in list.rhtml una specie di menu  
con tutte le categorie, e ognuna di queste sia un link\_to list\_cat.rhtml  
che visualizza tutti i prodotti presenti in quella categoria  
Qualcuno puÃ² aiutarmi?  
Grazie, baci a tutti  
Chiara

---

<div class="post-metadata">

**Author:** ![chiara79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chiara79](https://www.ruby-forum.com/u/chiara79)\
**Post date:** [January 12, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855/2 "2008-01-12T22:01:55Z")

</div>

p.s.

Come faccio a configurare il file routes.rb affinchÃ¨ il controller  
riesca a leggermi una richiesta cosÃ¬ strutturata:  
site/list\_cat/category\_id (cos’ funzionerebbe, ma dopo non mi funziona  
piÃ¹ list.rhtml la quale mi prende come parametro id e non category\_id

Grazie ancora

---

<div class="post-metadata">

**Author:** ![chiara79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chiara79](https://www.ruby-forum.com/u/chiara79)\
**Post date:** [January 15, 2008, 11:52am UTC](https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855/3 "2008-01-15T11:52:18Z")

</div>

Ciao Chiara per capire come funziona l’associazione alle categorie ti  
consiglio di seguire attentamente la guida che trovi al seguente  
indirizzo:

> **[Radar - O’Reilly](https://www.oreilly.com/radar/)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

per la personalizzazione degli url e del routes.rb alcune info le puoi  
trovare su di un mio precedente post:

[http://www.ruby-forum.com/topic/132470#new](http://www.ruby-forum.com/topic/132470#new)

---

<div class="post-metadata">

**Author:** ![chiara79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bb73d2/32.png) [@chiara79](https://www.ruby-forum.com/u/chiara79)\
**Post date:** [January 16, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/controller-vista-passaggio-variabile-e-routes-rb/125855/4 "2008-01-16T19:37:20Z")

</div>

Chiara B. wrote:

> p.s.
> 
> Come faccio a configurare il file routes.rb affinchÃ¨ il controller  
> riesca a leggermi una richiesta cosÃ¬ strutturata:  
> site/list\_cat/category\_id (cos’ funzionerebbe, ma dopo non mi funziona  
> piÃ¹ list.rhtml la quale mi prende come parametro id e non category\_id
> 
> Grazie ancora

Se non ho capito male dall’esempio del messaggio precedente, site Ã¨ il  
tuo controller (scrivevi APP/VIEW/SITE)  
Se Ã¨ cosÃ¬ non c’Ã¨ bisogno di configurare routes.rb  
Infatti con un’url tipo [http://www.server.com/site/list\_cat/category\_id](http://www.server.com/site/list_cat/category_id)  
category\_id finisce in params[:id] nel metodo list\_cat del controller  
site

Se invece site fosse il tuo server ([http://site/list\_cat/category\_id](http://site/list_cat/category_id))  
puoi usare questa rotta:

map.connect “list\_cat/:id”, :controller =\> “list\_cat”, :action =\> “cat”

che mette category\_id dentro a params[:id] e lo passa al metodo cat del  
controller list\_cat.

Infine, sistemato questo magari potresti vedere come usare  
link\_to\_remote al posto di link\_to, per aggiornare solo il pezzo di menu  
che vai ad espandere. Un po’ di AJAX non fa mai male 🙂
