I'm running into a dilemma here. The Firefox Add-Ons SDK only allows PUT and POST requests via its API. I am using the following route in Rails: "resource :users"
I can easily override the PUT to be:
"match 'users/:id' => 'users#update', :via => [:post, :put]
However, I can't do this again for DELETE as I also need to :post to the same URL again. Do I need to create a new route all-together and abandon the resourceful way of doing things? Is there a way to have POST behave like a DELETE on the Firefox side instead?
There is currently no way to do this in the high-level Request api, however you could implement something using the lower level xhr api:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.2/packages/api-utils/docs/xhr.html
This provides access to Firefox's underlying XmlHttpRequest implementation:
https://developer.mozilla.org/En/Using_XMLHttpRequest
Related
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
NOTE : Question reformulated to be more clear
I would like to simulate the sending of a form to an external domain in a controller action (like a redirect_to an external domain in POST with POST params). IE : Send the POST request with the params, and render the HTML results in the browser.
What is the best way to do it in Rails 3?
Sure, you can just execute the POST on the server side, collect the response and then do whatever you like with it. It's not unlike making a server side call to your favourite REST API and then rendering the response to the client.
You probably want to start with a library that provides a nice interface for server side POST. I'd suggest looking at https://github.com/nahi/httpclient
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.