Ruby on Rails MCQ Quiz Hub

Ruby on Rails MCQ

Choose a topic to test your knowledge and improve your Ruby on Rails skills

When rendering a partial in a view, how would you pass local variables for rendering?





✅ Correct Answer: 3

Within a Rails controller, which code will prevent the parent controller’s before_action :get_feature from running?





✅ Correct Answer: 1

Which statement correctly describes a difference between the form helper methods form_tag and form_for?





✅ Correct Answer: 3

What is before_action (formerly known as before_filter)?





✅ Correct Answer: 4

Which module can you use to encapsulate a cohesive chunk of functionality into a mixin?





✅ Correct Answer: 1

In Rails, which code would you use to define a route that handles both the PUT and PATCH REST HTTP verbs?





✅ Correct Answer: 3

Which choice includes standard REST HTTP verbs?





✅ Correct Answer: 1

Which ActiveRecord query prevents SQL injection?





✅ Correct Answer: 3

Given this code, which statement about the database table “documents” could be expected to be true? class Document < ActiveRecord::Base belongs_to :documentable, polymorphic: true end class Product < ActiveRecord::Base has_many :documents, as: :documentable end class Service < ActiveRecord::Base has_many :documents, as: :documentable end





✅ Correct Answer: 3

Are instance variables set within a controller method accessible within a view?





✅ Correct Answer: 1

When a validation of a field in a Rails model fails, where are the messages for validation errors stored?





✅ Correct Answer: 1

How would you generate a drop-down menu that allows the user to select from a collection of product names?





✅ Correct Answer: 4

For a Rails validator, how would you define an error message for the model attribute address with the message “This address is invalid”?





✅ Correct Answer: 4

Given the URL helper product_path(@product), which statement would be expected to be false?





✅ Correct Answer: 2

Given this code, which choice would be expected to be a true statement if the user requests the index action? class DocumentsController < ApplicationController before_action :require_login def index @documents = Document.visible.sorted end end





✅ Correct Answer: 4

In Rails, how would you cache a partial template that is rendered?





✅ Correct Answer: 1

What is the reason for using Concerns in Rails?





✅ Correct Answer: 1

When using an ActiveRecord model, which method will create the model instance in memory and save it to the database?





✅ Correct Answer: 3

You are using an existing database that has a table named coffee_orders. What would the ActiveRecord model be named in order to use that table?





✅ Correct Answer: 4

In ActiveRecord, what is the difference between the has_many and has_many :through associations?





✅ Correct Answer: 4

How do you add Ruby code inside Rails views and have its result outputted in the HTML file?





✅ Correct Answer: 3

How would you render a view using a different layout in an ERB HTML view?





✅ Correct Answer: 1

Where should you put images, JavaScript, and CSS so that they get processed by the asset pipeline?





✅ Correct Answer: 3

In Rails, what caching stores can be used?





✅ Correct Answer: 3

What is the correct way to generate a ProductsController with an index action using only the command-line tools bundled with Rails?





✅ Correct Answer: 3

If a model class is named Product, in which database table will ActiveRecord store and retrieve model instances?





✅ Correct Answer: 4

What is a popular alternative template language for generating views in a Rails app that is focused on simple abstracted markup?





✅ Correct Answer: 2

When Ruby methods add an exclamation point at the end of their name (such as sort!), what does it typically indicate?





✅ Correct Answer: 4

What part of the code below causes the method #decrypt_data to be run? class MyModel < ApplicationRecord after_find :decrypt_data end





✅ Correct Answer: 3

Which Rails helper would you use in the application view to protect against CSRF (Cross-Site Request Forgery) attacks?





✅ Correct Answer: 3

In the model User you have the code shown below. When saving the model and model.is_admin is set to true, which callback will be called?before_save :encrypt_data, unless: ->(model) { model.is_admin }after_save :clear_cache, if: ->(model) { model.is_admin }before_destroy :notify_admin_users, if: ->(model) { model.is_admin }





✅ Correct Answer: 1

In a Rails controller, what does the code params.permit(:name, :sku) do?





✅ Correct Answer: 2

Review the code below. Which Ruby operator should be used to fill in the blank so that the sort method executes properly? [5,8,2,6,1,3].sort {|v1,v2| v1 _ v2}





✅ Correct Answer: 3

There is a bug in this code. The logout message is not appearing on the login template. What is the cause? class AccessController < ActionController::Base def destroy session[:admin_id] = nil flash[:notice] = “”You have been logged out”” render(‘login’) end





✅ Correct Answer: 1

Which statement about ActiveRecord models is true?





✅ Correct Answer: 3

Which choice best describes the expected value of @result? @result = Article.first.tags.build(name: ‘Urgent’)





✅ Correct Answer: 2

If a product has a user-uploadable photo, which ActiveStorage method should fill in the blank? class Product << ApplicationRecord __ :photo end





✅ Correct Answer: 1

If the only route defined is resources :products, what is an example of a URL that could be generated by this link_to method? link_to(‘Link’, {controller: ‘products’, action: ‘index’, page: 3})





✅ Correct Answer: 1

Which part of the Rails framework is primarily responsible for making decisions about how to respond to a browser request?





✅ Correct Answer: 2

If User is an ActiveRecord class, which choice would be expected to return an array?





✅ Correct Answer: 1

What decides which controller receives which requests?





✅ Correct Answer: 4

When rendering a partial in a view, how would you pass local variables for rendering?





✅ Correct Answer: 2

Given this code, and assuming @user is an instance of User that has an assigned location, which choice would be used to return the user’s city? class Location < ActiveRecord::Base # has database columns for :city, :state has_many :users end class User < ActiveRecord::Base belovngs_to :location delegate :city, :state, to: :location, allow_nil: true, prefix: true end





✅ Correct Answer: 1

Where would this code most likely be found in a Rails project? scope :active, lambda { where(:active => true) }





✅ Correct Answer: 1

What is a standard prerequisite for implementing Single Table Inheritance (STI)?





✅ Correct Answer: 3

A way that views can share reusable code, such as formatting a date, is called a _?





✅ Correct Answer: 1

How do you add Ruby code inside Rails views and have its result outputted in the HTML file?





✅ Correct Answer: 4

You are working with a large database of portfolios that sometimes have an associated image. Which statement best explains the purpose of includes(:image) in this code? @portfolios = Portfolio.includes(:image).limit(20) @portfolios.each do |portfolio| puts portfolio.image.caption end





✅ Correct Answer: 4

After this migration has been executed, which statement would be true? class CreateGalleries < ActiveRecord::Migration def change create_table :galleries do |t| t.string :name, :bg_color t.integer :position t.boolean :visible, default: false t.timestamps end end end





✅ Correct Answer: 2

Given two models, what is the issue with the query used to fetch them? class LineItem < ApplicationRecord end class Order < ApplicationRecord has_many :line_items end Order.limit(3).each { |order| puts order.line_items }





✅ Correct Answer: 2