I am new to ruby, i would like to pass parameters in the url and receive it in controller.
I am expecting operation like
www.mysite.com/getuser/id/22
where getuser is the param name and 22 is its value.
Please provide me if there is any useful links that i can refer to.
Thanks in advance
Please read everything in the Rails Guide to Routing . There are two main cases there:
RESTful routes: only use GET, PUT, POST, DELETE. Rails maps that by using the method resources
. resources :pages
will lead to the following routes (and URLs) automatically:
sites GET /sites(.:format) {:action=>"index", :controller=>"sites"} POST /sites(.:format) {:action=>"create", :controller=>"sites"} new_site GET /sites/new(.:format) {:action=>"new", :controller=>"sites"} edit_site GET /sites/:id/edit(.:format) {:action=>"edit", :controller=>"sites"} site GET /sites/:id(.:format) {:action=>"show", :controller=>"sites"} PUT /sites/:id(.:format) {:action=>"update", :controller=>"sites"} DELETE /sites/:id(.:format) {:action=>"destroy", :controller=>"sites"}
Other routes: There is a rich set of methods you can use in your routes file to add additional routes. But keep in mind: If you just want to address a resource, it is better to stick to restful routes. A typical example is_ match ':controller(/:action(/:id))'
. This allows URLs like:
localhost:3000/sites/help
: controller == SitesController
, action == help
localhost:3000/sites/search/something
: controller == SitesController
, action == search
, parameter in params
is something
under the key id
. So inside the action search
, you will find params[:id]
bound to "something"
. In your config/routes.rb file you write:
resources :users
Then you create the controller:
rails g controller users
Inside your controller file you have something(contrived) there like :
def show
my_id_param = params[:id]
end
When you go to: www.mysite.com/user/22
my_id_param will be 22
I think rails guides are quite good resource, just google for them, has ton of good info there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.