Ruby on Rails Quiz
Fast practice, instant feedback. Timer auto-submits when time’s up.
Avg score: 26% Most missed: “Given this Category model with an attribute for 'name', what code would fill in …”

Ruby on Rails MCQs For LinkedIn Skill Assessments.

Ruby on Rails Quiz
Time left 00:00
25 Questions

1. How would you render a view using a different layout in an ERB HTML view?
2. Given this controller code, which choice describes the expected behavior if parameters are submitted to the update action that includes values for the product's name, style, color, and price?
rb
class ProductController < ActionController::Base
def update
@product = Product.find(params[:id])
if @product.update_attributes(product_params)
redirect_to(product_path(@product))
else
render('edit')
end
end
private
def product_params
params.require(:product).permit(:name, :style, :color)
end
end
3. If the Rails asset pipeline is being used to serve JavaScript files, how would you include a link to one of those JavaScript files in a view?
4. Which Rails helper would you use in the application view to protect against CSRF (Cross-Site Request Forgery) attacks?
5. In Rails, which code would you use to define a route that handles both the 'PUT' and 'PATCH' 'REST HTTP' verbs?
6. 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
7. 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?
rb
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 }
8. When Ruby methods add an exclamation point at the end of their name (such as 'sort!'), what does it typically indicate?
9. Which choice is an _incorrect_ way to render a partial?
10. What is the correct way to assign a value to the session?
11. Given a table of blog_posts and a related table of comments (comments made on each blog post), which ActiveRecord query will retrieve all blog posts with comments created during @range?
12. What part of the code below causes the method '#decrypt_data' to be run?
rb
class MyModel < ApplicationRecord
after_find :decrypt_data
end
13. Which code would you add to return a 404 to the API caller if the user is not found in the database?
ruby
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
render json: @user, status: :ok,
# Missing code
end
14. Are instance variables set within a controller method accessible within a view?
15. For a Rails validator, how would you define an error message for the model attribute 'address' with the message 'This address is invalid'?
16. Which choice includes standard REST HTTP verbs?
17. What is the correct way to generate a ProductsController with an index action using only the command-line tools bundled with Rails?
18. If a model class is named Product, in which database table will ActiveRecord store and retrieve model instances?
19. Within a Rails model with a 'cache_key' method, which code snippet will expire the cache whenever the model is updated?
20. What is the reason for using Concerns in Rails?
21. Which statement correctly describes a difference between the form helper methods 'form_tag' and 'form_for'?
22. How would you validate that a project's name is not blank, is fewer than 50 characters, and is unique?
23. Given this code, which choice would be expected to be a _true_ statement if the user requests the index action?
rb
class DocumentsController < ApplicationController
before_action :require_login
def index
@documents = Document.visible.sorted
end
end
24. You made a spelling mistake while creating a table for bank accounts. Which code would you expect to see in a migration to fix the error?
25. Given this code, which statement about the database table 'documents' could be expected to be _true_?
rb
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

⚡ Recently practiced quizzes in this topic
Live quiz activity