Ruby on Rails MCQ Quiz Hub

Ruby on Rails MCQ

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

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




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




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




4. What is before_action (formerly known as before_filter)?




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




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




7. Which choice includes standard REST HTTP verbs?




8. Which ActiveRecord query prevents SQL injection?




9. 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




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




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




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




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




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




15. 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




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




17. What is the reason for using Concerns in Rails?




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




19. 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?




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




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




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




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




24. In Rails, what caching stores can be used?




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




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




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




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




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




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




31. 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 }




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




33. 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}




34. 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




35. Which statement about ActiveRecord models is true?




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




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




38. 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})




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




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




41. What decides which controller receives which requests?




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




43. 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




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




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




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




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




48. 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




49. 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




50. 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 }