Two questions (url/route and ajax)

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 :slight_smile:

anyone? :frowning:

Up :’(

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?

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

anyone know for the ajax call? :frowning:

Matthew R. wrote:

thanks, i’ll try :slight_smile:
what about the ajax call? :frowning:

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

On 7/21/07, Mix M. [email protected] wrote:

anyone know for the ajax call? :frowning:


Posted via http://www.ruby-forum.com/.


Obie F.

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