UrbanPro
true

Learn Ruby on Rails Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

50 Most Common Ruby on Rails Developer Mistakes: Controllers

Pawan Yadav
22/04/2017 0 0

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
view rawvar.rb hosted with ? by GitHub

 

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
view rawdes.rb hosted with ? by GitHub

 

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
view rawpar.rb hosted with ? by GitHub

 

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

Migration from other languages to Salesforce
Anyone can easily migrate from other language to Salesforce. People must have skills to understand business logic.

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
X

Looking for Ruby on Rails Training Classes?

The best tutors for Ruby on Rails Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Ruby on Rails Training with the Best Tutors

The best Tutors for Ruby on Rails Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more