Pointy haired boss request - turn off pretty url

So the app I’m working on has things that we refer to with a slug (let’s
pretend they are blog entries). If one has the proper URL of the form
(example) www.mydomain.com/blogthing/display/666, they can enter that in
their browser, and the rails app will dutifully go there and display
blogthing number 666. All is well.

Now suppose somebody is browsing the blogthings and happens upon 666.
He decides to send the URL for blogthing 666 to his buddy, and goes to
the URL box of the browser, prepared to copy it and send it to his
buddy. Low and behold, the URL box in the browser doesn’t say
www.mydomain.com/blogthing/display/666...is just says
www.mydomain.com/blogthing/display.

This is because the routing for www.mydomain.com/blogthing/display/666
takes the slug off the end and passes the slug as a parameter to the
“blogthing controller’s display action”, which then shows the blogthing
entry in question.

So the pointy haired boss wants this case to result in
www.mydomain.com/blogthing/display/666” in the browser’s URL box. The
other reason for this is that if one wants to bookmark a particular
blogthing, it needs to have the whole thing in the URL box of the
browser. Maybe not so pointy haired after all.

Is there any way to do this without hacking the Rails internals?

If not, dare I say…how/where would I do this hacking?

thanks!

jp

post your routes please because I have a hunch.
(all routes not just the ones you think are taking effect)
and an example hash that you are passing to link_to

ps what do you mean by slug

This is because the routing for www.mydomain.com/blogthing/display/666
takes the slug off the end and passes the slug as a parameter to the
“blogthing controller’s display action”, which then shows the blogthing
entry in question.

/blogthing/display/666 should be accessing the display action, passing
666 as the slug unless you’re doing something weird. In which case,
I’d stop doing that weird thing.


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Keynan P. wrote:

post your routes please because I have a hunch.
(all routes not just the ones you think are taking effect)
and an example hash that you are passing to link_to

Keynan, I can’t do that for various reasons.

What is your hunch?

thanks,
jp

P.S. A “slug” is the record-specific thing on the end of a url that
says which one of these things you’re after. It might must be the id of
the item, or it might be some sort of a mashup to make it look better or
to make it opaque. In the end, the slug is something that can be turned
into an ID and used to find a specific entry in the db.

Rick O. wrote:

/blogthing/display/666 should be accessing the display action, passing
666 as the slug unless you’re doing something weird. In which case,
I’d stop doing that weird thing.


Rick O.

Rick,
It is indeed doing as you say, but the URL box in the browser only shows
http://www.mydomain.com/blogthing/display

It doesn’t have the 666 on the end. That means I can’t book mark this
blog entry or copy and paste the url to send it to someone.

thanks,
jp

On 6/4/07, Jeff P. [email protected] wrote:

were being used to map this thing. Shouldn’t there be a way to have the
would that show up in the URL box after routing as:
http://www.mydomain.com/a/b/1
???

You’re removing it somewhere, so stop removing it. Rails doesn’t hide
parameters or anything. Pretty much every rails site in existence
uses some form of slug (usually an ID, but the implementation in the
code is identical).


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Jeff,

as Rick pointed out, the 666 should show by default as that is how Rails
handles it from the start. Lets take a basic scaffold example,

the

domain.com/foo

would be an index/list

and

domain.com/foo/1

would be a show page. Are you doing something that makes the 666 into
something else?

John H.

On 6/4/07, Jeff P. [email protected] wrote:


“Work hard to find something that fascinates you.” - Richard Feynman

Rick O. wrote the following on 04.06.2007 18:43 :

map.connect ‘:controller/:action/:id’
http://www.mydomain.com/a/b/1

Just a ‘hunch’ of mine: do you use button_to or any other way of
generating a form in the page to generate a button leading to this URL?
Then it is probably the source of your problem, the “slug” as you call
it is probably passed as a CGI parameter in the POST content, not in the
URL.

If that’s the case, the simplest solution would be to use link_to. If
your routes aren’t ok you’ll have a “?slug=” in the URL
instead of the cleaner pretty URLs, if that’s the case, as said
previously, show us the routes. Without them we can only throw wild
guesses around.

If you need the link to look like a button, just use CSS. Using a form
to generate a link to a display page is bad practice: you are on the way
to get nested forms (which are inherently broken). Only use forms to
modify data.

Lionel.

Keynan P. wrote:

post your routes please because I have a hunch.
(all routes not just the ones you think are taking effect)
and an example hash that you are passing to link_to

for grins, let’s say that the default route:

Install the default route as the lowest priority.

map.connect ‘:controller/:action/:id’

were being used to map this thing. Shouldn’t there be a way to have the
slug show up in the URL box?

In this forum for instance, navigating to this topic gives a URL of:
http://www.ruby-forum.com/topic/110551#new

That 110551 is the slug. In my app as currently written, that would not
be there. Am I doing something wrong? In a “normal” rails app where
the default route is routing:
http://www.mydomain.com/a/b/1

would that show up in the URL box after routing as:
http://www.mydomain.com/a/b/1
???

thanks,
jp

What you’re saying implies that this is a redirect and not a render of
that action. Please confirm what you wrote:

  1. You request GET /blogthing/display/666

  2. You arrive at POST /blogthing/display with params[:slug] = ‘666’

Rails does no such magic unless you are in fact doing a weird thing.

–Andrew V.

On Jun 4, 7:53 am, Jeff P. [email protected]

werdnativ wrote:

What you’re saying implies that this is a redirect and not a render of
that action. Please confirm what you wrote:

  1. You request GET /blogthing/display/666

  2. You arrive at POST /blogthing/display with params[:slug] = ‘666’

Rails does no such magic unless you are in fact doing a weird thing.

–Andrew V.

On Jun 4, 7:53 am, Jeff P. [email protected]

Thanks everybody. I’ll study it tonight and see what weird thing I’m
doing. I don’t remember doing a weird thing, but then one man’s weird
thing…

jp

Jeff P. wrote:

Thanks everybody. I’ll study it tonight and see what weird thing I’m
doing. I don’t remember doing a weird thing, but then one man’s weird
thing…

jp

OK, the “weird thing” I was doing (no, not that, I mean in the code) is
that I was doing a redirect to a different action. In hindsight, I
should have expected that to screw up the stuff in the URL box.

This is my second significant Rails effort. I’ve traced this error back
to something my mentor during the first Rails project said but never
explained. He (inadvertently I’m sure) put me in the habit of using
redirects all over the place for no good reason.

Just one more step in that long lonely walk from newbie to rock star.

Thanks for making it clear to me that i was doing something weird.

jp

(…Mommy, he’s doing it again! …)

I’d presume that your not passing the slug as an id, and your passing
it as a :slug => object.slug or whatever.
Just change that to :id => object.slug(or whatever method it is on the
object) this should automatically populate your URL in the :id space,
as in :controller/:action/:id and then use params[:id] in your
controller…

OR

You could create a named route something like
map.restful_name ‘blogthing/display/:slug’, :controller =>
controller_name, :action => action_name
Your form should then use, :slug(in place of :id) so :slug =>
object.slug
Then you can use params[:slug] in your controller

Does this make sense?
Basically in both examples the same thing is happening, the :id/:slug
is put into the URL by the form, and then the :id/:slug is pulled out
of the URL by the controller, so if you just go straight to the URL
the controller still finds the id/slug.

Cam

On Jun 5, 1:05 pm, Jeff P. [email protected]

Also check out the magic routing plugin, it might be suitable for your
URLs with meaningful slugs:

http://agilewebdevelopment.com/plugins/magic_routing

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs