开发者

URL rewriting in ROR

开发者 https://www.devze.com 2023-01-07 19:58 出处:网络
I am Amit. i am new to Rails. Please forgive me if ask any stupid questions. I have gone through this article. I am also suffering with the same

I am Amit. i am new to Rails. Please forgive me if ask any stupid questions.

I have gone through this article. I am also suffering with the same problem.

my website URL like this: locahost:3000/users/edit/30

I don't want to show the controller:users and action: edit.

I want to Re-Write (rewrite) the URL or i want to maintain the URL as http://127.0.0.0:3000/30/ only.(30 is a user id)

I am not interested to show the controller(user) and action (edit)

I total intention is to Hiding (HIDING) and rewriting (REWRING) the URL and mainly i want to hid开发者_开发百科e the URL Extensions with controller and actions mainly..


It's an odd requirement to want to use a route this way as it will make it difficult for you to expand this scheme to support other actions in your application. One of the advantages of Rails conventions done as they are is that you usually don't have to worry about (often trivial) application details or have to have strong opinions about them.

But it you really, really want to, you can add this to your config/routes.rb

ActionController::Routing::Routes.draw do |map|
  map.connect "/:id", :controller => "users", :action => "edit"
end

Remember that this limited scheme will mean you can only route to the edit action. Not terribly useful I would suggest.


The rails routes.rb file is your answer:

Lots of information here on how to do exactly what you need:

http://guides.rails.info/routing.html#customizing-resourceful-routes


For most application I have been using the standard:

/script.cgi?arg1=foo&arg2=bar

Now obviously /user/edit/eric looks much better for a URL that involves editing the "Eric" user. But how does this work out when you have multiple items. For example if I want to delete a group of users. I might have something like this in the old way:

/user/delete.cgi?id=3&id=5&id=7

or for a more readable format we could have:

/user/delete.cgi?username=eric&username=john&username=paul

This of course assumes that usernames are unique. :) So it seems that I would want something like:

/user/delete/eric/john/paul

This makes it obvious what is happening from the url. But how do I generate this? And if a action receives this request how does this get parsed so I have something like:

@delete_usernames ===>>> [ 'eric', 'john', 'paul' ]

This way I can then load up those users and delete them. I know that ActionController provides some methods for dealing with URL rewriting but I am unsure how to best use them. Any help or pointers are greatly appreciated.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号