Forum: Ruby on Rails Two questions (url/route and ajax)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mix M. (Guest)
on 2007-07-10 20:47
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 :)
Mix M. (Guest)
on 2007-07-12 15:13
anyone? :(
Mix M. (Guest)
on 2007-07-14 23:23
Up :'(
Matthew Rudy J. (Guest)
on 2007-07-14 23:38
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?
Matthew Rudy J. (Guest)
on 2007-07-14 23:39
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
Mix M. (Guest)
on 2007-07-14 23:53
Matthew R. wrote:
> ...

thanks, i'll try :)
what about the ajax call? :(
Mix M. (Guest)
on 2007-07-21 21:42
anyone know for the ajax call? :(
Obie F. (Guest)
on 2007-07-26 02:45
(Received via mailing list)
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 http://www.jroller.com/obie/entry/seo_optimization...

On 7/21/07, Mix M. <removed_email_address@domain.invalid> wrote:
>
> anyone know for the ajax call? :(
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Obie F.
http://jroller.com/obie/

Pre-order my book The Rails Way today!
http://www.amazon.com/dp/0321445619
This topic is locked and can not be replied to.