# Two questions (url/route and ajax)

**URL:** <https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024>\
**Category:** Rails\
**Created:** [July 10, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024 "2007-07-10T18:47:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 10, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/1 "2007-07-10T18:47:29Z")

</div>

Hi, i’ve just 2 questions. The first is about the url, now i can see a  
page with site/products/1 but how can i do something prettier like  
site/products/1-my-first-product (with a product named: “My first  
product”) ?  
What have i to change and how?  
The second question is about ajax, i’ve a select with some categories  
with more levels (e.g. A, A1, A2, B, B1, B11, B12, and so on…A1 and A2  
are children of A, as B11 and B12 are for B1 which is for B). How can i  
do something that when i select the category A, it’ll load the 2  
children A1 and A2 ? Now i’ve set that when i send the form it load the  
children and render again the form, select one of them and send the form  
again, it’s quit too much for just a send of a product. So, how can i  
load them with ajax?  
Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 12, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/2 "2007-07-12T13:13:32Z")

</div>

anyone? ☹

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 14, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/3 "2007-07-14T21:23:50Z")

</div>

Up :’(

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 14, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/4 "2007-07-14T21:38:04Z")

</div>

the way one would normally do this is to add a “permalink” column to the  
database.

for your purpose, we’d use and after\_create, but it seems inefficient to  
do 2 database queries

class Product  
after\_create :set\_permalink

```
def set_permalink
    hyphenated_name = self.name.downcase.gsub(/\s+/, "-")
    self.permalink = "#{self.id}-#{hyphenated_name}"
end

```

end

then in your routes.rb

map.permalink(“products/:permalink”,  
:controller =\> “products”  
)

and in your product\_controller

```
def show
    @product = if params[:permalink]
        Product.find_by_permalink(params[:permalink])
    else
        Product.find(params[:id])
    end
end

```

Mix M. wrote:

> Hi, i’ve just 2 questions. The first is about the url, now i can see a  
> page with site/products/1 but how can i do something prettier like  
> site/products/1-my-first-product (with a product named: “My first  
> product”) ?  
> What have i to change and how?

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 14, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/5 "2007-07-14T21:39:12Z")

</div>

you’d have to save that aswell

def set\_permalink  
hyphenated\_name = self.name.downcase.gsub(/\s+/, “-”)  
self.update\_attribute(:permalink, “#{self.id}-#{hyphenated\_name}”)  
end

> class Product  
> after\_create :set\_permalink
> 
> ```
> def set_permalink
> hyphenated_name = self.name.downcase.gsub(/\s+/, "-")
> self.permalink = "#{self.id}-#{hyphenated_name}"
> end
> 
> ```
> 
> end

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 21, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/6 "2007-07-21T19:42:04Z")

</div>

anyone know for the ajax call? ☹

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 14, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/7 "2007-07-14T21:53:23Z")

</div>

Matthew R. wrote:

> …

thanks, i’ll try 🙂  
what about the ajax call? ☹

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/two-questions-url-route-and-ajax/109024/8 "2007-07-26T00:45:52Z")

</div>

Not too long ago, I wrote a popular blog post about getting pretty  
1-this-is-a-pretty-url style using Rails’ to\_param method.

See [Best Online Casino in Australia | Top Licensed Casinos for Gamblers](http://www.jroller.com/obie/entry/seo_optimization_of_urls_in)

On 7/21/07, Mix M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> anyone know for the ajax call? ☹
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Obie F.

> **[Best Online Casino in Australia | Top Licensed Casinos for Gamblers](https://toponlinecasinoaustralia.com)**
>
> Best Online Casino in Australia with licensed games only. Play blackjack, roulette, poker and other top-notch casino games. Enjoy gambling 24/7.

Pre-order my book The Rails Way today!  
[http://www.amazon.com/dp/0321445619](http://www.amazon.com/dp/0321445619)
