Making Code Using Javascript For Dependent Selects Restful
Solution 1:
If I had to describe if, being RESTful means that:
- You provide meaningful resources names
- You use the HTTP verbs to express your intents
- You make proper use of HTTP codes to indicate status
Provide meaningful resource names
As you probably heard it before, everything in REST is about resources. But from the outside, it's just the paths you expose. Your resources are then just a bunch of paths such as:
GET /burgers # a collection of burgers
GET /burger/123 # a burger identified with id 123
GET /burger/123/nutrition_facts # the nutrition facts of burger 123
POST /burgers # with data: {name: "humble jack", ingredients: [...]} to create a new burger
PUT /burger/123 # with data: {name: "chicken king"} to change the name of burger 123
For instance, if you had a path with the url
GET /burger_list?id=123
That would not be considered good practice.
It means you need to think hard about the names you give your resources to make sure the intent is explicit.
Use HTTP verbs to express your intents
It basically means using:
- GET to read a resource identified by an identifier (id) or a collection of resources
- PUT to update a specific resource that you identify by an identifier (id)
- DELETE to destroy a specific resource that you identify by an id
- POST to create a new resource
Usually, in Rails, those verbs are, by convention, used to map specific actions in your controller.
- GET goes to show or index
- PUT goes to update
- DELETE goes to destroy
- POST goes to create
That's why people usually say that if you have actions in your controllers that don't follow that pattern, you're not "RESTful". But in the end, only the routes you expose count. Not really your controller actions. It is a convention of course, and conventions are useful for readability and maintainability.
You make proper use of HTTP codes to indicate status
You already know the usual suspects:
- 200 means OK, everything went fine.
- 404 means NOT FOUND, could not find resource
- 401 means UNAUTHORIZED, authentication failed, auth token invalid
- 500 means INTERNAL SERVER ERROR, in other words: kaput
But there are more that you could be using in your responses:
- 201 means CREATED, it means the resource was successfully created
- 403 means FORBIDDEN, you don't have the privileges to access that resource
- ...
You get the picture, it's really about replying with the right HTTP code that represents clearly what happens.
Answering your questions
would that be accurate that it is not RESTful?
From what I see, the first issue is your path.
post 'cars/make_list', to: 'cars#make_list'
What I understand is that you are retrieving a collection of car makes. Using a POST to retrieve a collection is against REST rules, you should be using a GET instead. That should answer your first question.
how does that impact my application?
Well, the impact of not being restful in your case is not very big. It's mainly about readability, clarity and maintainability. Separating concerns and putting them in the right place etc... It's not impacting performance, nor is it a dangerous issue. You're just not RESTful and that makes it more complicated to understand your app, in your case of course.
what should/could I do to resolve that?
Besides the route problem, the other issue is that your action is called make_list and that doesn't follow Rails REST conventions. Rails has a keyword to create RESTful routes:
resources :car_makes, only: [:index] # GET /car_makes , get the list of car makes
This route expresses your intent much better than the previous one and is now a GET request. You can then use query parameters to filter the results. But it means we need to create a new controller to deal with it.
classCarMakesController < ApplicationControllerdefindex
makes = params[:year].blank? ? "" : YmmMake.where(ymm_year_id: params[:year]).order(:make)
render partial:"makes", locals: {car: params[:car], form: params[:form], makes: makes}
end
private
# Strong parameters stuff...end
And of course we also need to change your jquery to make a GET request instead of a POST.
$(document).ready(function () {
...
// when the #year field changes
$("#car_ymm_year_id").change(function () {
// ...
$.get({
url: '/car_makes',
data: {
form: form,
year: year,
car: car
},
success: function (data) {
$("#car_ymm_make_id").html(data);
});
returnfalse;
});
...
});
This is a much better solution, and it doesn't require too much work.
There is an excellent tutorial on REST on REST API tutorial, if you want to know more about the specifics. I don't know much about the small details, mostly what is useful on a day to day basis.
Hope this helps.
Post a Comment for "Making Code Using Javascript For Dependent Selects Restful"