I'm wanting to use a short url similar to a short url from bit.ly. Currently i have resources :clips instead of accessing clips/:id in my route i want to access it from my url_hash field on my Clips model. Am I going to have to manually list the routes to do this?
I would use the slugged gem:
https://github.com/Sutto/slugged
or (more popular):, friendly_id
https://github.com/norman/friendly_id
Related
I have an existing website which I am attempting to port over to rails (3.2.7) and need to maintain the current urls.
Current website has urls like this:
http://example.com/Joe
http://example.com/Bob
Using rails the closest I have come is using the friendly_id gem and get this:
http://example.com/users/Joe
http://example.com/users/Bob
Every example I find seems to include the controller name in the url. How can I generate urls like the existing website?
Assuming you have :resources :users somewhere in your routes.rb you can put the next route definition in file:
match '/:name' => "users#show"
This way the url /Joe will direct to UsersController show action, populating params[:name] with the string 'Joe'.
You can find all the configuration steps needed here, 'Removing the controller names from URLs'
When connecting to an external json api and submitting a form to update a resource is it better to use form_for or form_tag ?
Specifically I'm using the Shopify API http://api.shopify.com/
In config/routes.rb I made default resource routes with resources :variants and now I'm trying to make a form that updates a variant resource but can't configure the form to have the proper action.
==== Update ====
Yes there's a shopify API gem: https://github.com/Shopify/shopify_api that does most of the heavy lifting- just can't quite figure out how to make it work.
To update an #variant object I need to PUT here: PUT /admin/variants/#{id}.json
But I can't quite construct the form_tag properly. I have these routes:
rake routes:
variants GET /variants(.:format) variants#index
POST /variants(.:format) variants#create
new_variant GET /variants/new(.:format) variants#new
edit_variant GET /variants/:id/edit(.:format) variants#edit
variant GET /variants/:id(.:format) variants#show
PUT /variants/:id(.:format) variants#update
DELETE /variants/:id(.:format) variants#destroy
Does your app define the Model? If so, you should be able to use form_for. If not, then you have to use form_tag.
form_for takes a Model instance as parameter (hence the name). Output of form fields is more concise, since form_for can infer a lot about from the model. You can also use fields_for to do nested forms.
If you use form_tag, then you need to write more code to properly construct the HTTP parameters so that Rails can reconstruct the params hash on the server.
I am new to rails. I have developed an application on rails recently. The application is pretty big and it's running fine. Currently i have url like this.
http://192.168.99.220/user/13/domainUsers
I want it to be like the below one (without any id)
http://192.168.99.220/user/domainUsers
My routes are like this.
match 'user/:id/domainUsers', :to => 'domains#manageDomain_2', :as => :manageDomain2
I have tried to rewrite the url using "to_param". As my application is too big and it has lots of functionalities, i am using parameters other than the "id" to find users informations frequently, so i am not being able to use the "to_param" method. Is there any other way to hide "id" from url.
Please help
Thanks in advance.
The easiest way to do this is with a gem called friendly_id.
Here is a tutorial that explains it quite well:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
We have an app with a large number of non-RESTful, verb-oriented controllers. I remember that long ago, before Rails got the REST religion, you could get automatic routes and helpers for those. Is there any way to still do this? It's a pain to keep adding GETs every time we add an action, and our app (or perhaps our collective development brain) just doesn't lend itself to RESTfulness.
You can use the "magic route", I believe it's still in the routes file by default, but if you don't have it here it is:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
You can still use a default route like this:
match ':controller(/:action(/:id))'
to match paths like
/monkey/play
/monkey/see/1
/monkey/hear/1
/monkey/eat/1
Actually there is any easy way to prepend '/#!/' name prefix to all routes in rails 3?
like twitter and facebook
http://twitter.com/#!/username
I need that to manage ajax request.
Thanx very much
I doubt there's a built-in way to achieve it, though it wouldn't be impossible to write a JavaScript helper to turn to the server for new requests with #!/ left out from the URLs, and catch all click events to turn them into URL hashes.