views:

1435

answers:

3

I would like to make my application somewhat REST compliant. I am using Rails on the backend and GWT on the frontend. I would like to do updates and deletes. I realize I can do something like mydomain.com/:id/delete (GET) and accomplish the same thing. However, as I stated previously, I would like to have a REST compliant backend. Thus, I want to do mydomain.com/:id (DELETE) and have it implicitly call my delete method.

Now, it's my understanding that if a browser (my browser is GWT RequestBuilder) doesn't support DELETE/GET, Rails somehow accomplishes this task with a POST and some other url parameter. So, how can I accomplish this with a GWT RequestBuilder?

Thanks,

JP

+3  A: 

Rails does this with hidden attributes. The easiest way to figure this out would be to create a new rails application, generate a scaffold and have a look at the HTML in a browser.

Try this:

rails jp
cd jp
./script/generate scaffold RequestBuilder name:string
rake db:migrate
./script/server

Then navigate to http://localhost:3000/request_builders, click on New and have a look at the HTML. You'll see something like:

<form action="/request_builders" class="new_request_builder" 
  id="new_request_builder" method="post">
  <div style="margin:0;padding:0">
    <input name="authenticity_token" type="hidden" value="e76..." />
  </div>

This is a creation, method is POST. Enter a name, save then Edit:

<form action="/request_builders/1" class="edit_request_builder" 
  id="edit_request_builder_1" method="post">
  <div style="margin:0;padding:0">
    <input name="_method" type="hidden" value="put" />
    <input name="authenticity_token" type="hidden" value="e76..." />
  </div>

Of course the form is sent with POST, but Rails hads a hidden field to simulate a PUT request. Same for deletion, but the scaffold will do it with a bit of Javascript:

var m = document.createElement('input'); 
m.setAttribute('type', 'hidden'); 
m.setAttribute('name', '_method'); 
m.setAttribute('value', 'delete');

To have this work with another front-end, you'll have to both:

  • Use the same style URL such as /request_builders/1 (RESTful URLs)
  • Include the hidden fields (Rails trick)
Christian Lescuyer
Unfortunately, this cannot be considered a complete solution, since it works only for URL encoded data (forms), and does not work with JSON or XML, which is more common way of doing requests with GWT
skrat
A: 

Christian,

RequestBuilder is not a controller, but a class in GWT.

With that being said, you still answered my question. I was looking for the additional hidden parameter. Thus, I just need to append "_method=PUT" or "_method=DELETE." I should have been a bit more clear.

Thanks!

JP
A: 

Hi JP, Im trying to build an similar kind of application. can you help me out with this. how the gwt can be integrated in a rails app.

Thanks Hansraj

hansraj
Hansraj, I would recommend that you start a new question and detail the problems you are having there. I would be happy to provide insight.
JP