They use instance variables in controllers
Quite often beginners declare all the variables in controllers as instances even if it may not be required. The problem with instance variables is that if you make a mistake, you won’t get the error message. A good practice here is to use one or two instance variables per action. If you need more, group them in Presenter.
############### | |
## WRONG ## | |
############### | |
classAPI::ArticlesController | |
defindex | |
@articles=Article.all | |
render json: @articles, status: 200 | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
classAPI::ArticlesController | |
defindex | |
articles = Article.all | |
render json: articles, status: 200 | |
end | |
end |
They don’t specify the HTTP response status in API controllers
Designing API you must specify the response status. Thus, the client application is able to know about the previous actions before processing the response body.
############### | |
## WRONG ## | |
############### | |
classAPI::ArticlesController | |
defcreate | |
article = Article.new(article_params) | |
if article.save | |
render json: article | |
else | |
render json: { errors: article.errors.full_messages } | |
end | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
classAPI::ArticlesController | |
defcreate | |
article = Article.new(article_params) | |
if article.save | |
render json: article, status: 201 | |
else | |
render json: { errors: article.errors.full_messages }, status: 422 | |
end | |
end | |
end |
Initialize or look for objects without parent connections
The current object’s parent can often be available in the controller. For example, the user should be able to edit only his own objects in the personal account. The range of lessons that can be found is limited by the given course.
Using this simple approach you can make the app more stable and increase its security. What is more important, it will help you to avoid an enormous amount of errors. This approach applies not only to personal accounts but also to nested routes. As soon as you’ve found the parent, look for the child object.
############### | |
## WRONG ## | |
############### | |
classUsers::CoursesController | |
defedit | |
@course=Course.find(params[:id]) | |
end | |
end | |
classUsers::LessonsController | |
defnew | |
@lesson=Lesson.new(lesson_params) | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
classUsers::CoursesController | |
defedit | |
@course= current_user.courses.find(params[:id]) | |
end | |
end | |
classUsers::LessonsController | |
defnew | |
course = current_user.courses.find(params[:course_id]) | |
@lesson= course.lessons.build(lesson_params) | |
end | |
end |
They use instance variables in partials
The problem with using instance variables in partials is that as soon as the project starts growing everything turns into a mess. It will be really hard to tell which variable came from where. Thus, supporting all of the dependencies becomes impossible.
############### | |
## WRONG ## | |
############### | |
= render 'form' | |
# _form.html.haml: | |
= simple_form_for [:users, @course] do |f| | |
-# some form | |
############### | |
## RIGHT ## | |
############### | |
= render 'form', course: course | |
# _form.html.haml: | |
= simple_form_for [:users, course] do |f| | |
-# some form |
Other Lessons for You
Find Ruby on Rails Training classes near you
- Ruby on Rails Training classes in Bangalore
- Ruby on Rails Training classes in Hyderabad
- Ruby on Rails Training classes in Gurgaon
- Ruby on Rails Training classes in Chennai
- Ruby on Rails Training classes in Delhi
- Ruby on Rails Training classes in Pune
- Ruby on Rails Training classes in Indore
- Ruby on Rails Training classes in Noida
- Ruby on Rails Training classes in Mumbai
Looking for Ruby on Rails Training classes?
Learn from Best Tutors on UrbanPro.
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near you