Last active
June 20, 2017 13:11
-
-
Save anoobbava/970d74adc882eb5f3b961230ac49591d to your computer and use it in GitHub Desktop.
mistakes controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use variables as instance variables when used. | |
---------------------- | |
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 | |
############### | |
## WRONG ## | |
############### | |
class API::ArticlesController | |
def index | |
@articles = Article.all | |
render json: @articles, status: 200 | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
class API::ArticlesController | |
def index | |
articles = Article.all | |
render json: articles, status: 200 | |
end | |
end | |
specify the Response in Api controllers: | |
-------------------------- | |
############### | |
## WRONG ## | |
############### | |
class API::ArticlesController | |
def create | |
article = Article.new(article_params) | |
if article.save | |
render json: article | |
else | |
render json: { errors: article.errors.full_messages } | |
end | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
class API::ArticlesController | |
def create | |
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 | |
limit Use of instance Variable in Partials: | |
------------------- | |
############### | |
## 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 | |
Always think about the before_action: | |
---------------- | |
############### | |
## WRONG ## | |
############### | |
class Users::CourseController | |
def update | |
if current_user.has_role?(‘trainer’) | |
@course = current_user.courses.find(params[:id]) | |
if @course.save | |
redirect_to courses_path | |
else | |
render :edit | |
end | |
else | |
redirect_to root_path, notice: ‘You are not authorized’ | |
end | |
end | |
end | |
############### | |
## RIGHT ## | |
############### | |
class Users::CourseController | |
before_action :ensure_user_has_access | |
before_action :find_course | |
def update | |
if @course.save | |
redirect_to courses_path | |
else | |
render :edit | |
end | |
end | |
private | |
def ensure_user_has_access | |
unless current_user.has_role?(‘trainer’) | |
redirect_to root_path, notice: ‘You are not authorized’ | |
end | |
end | |
def find_course | |
@course = current_user.courses.find(params[:id]) | |
end | |
end | |
Use helpers to hide code from views: | |
-------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment