Ruby On Rails

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

1.

`<%= render partial: "nav", selected: "about"}%>`

2.

`<%= render partial: "nav", local_variables: {selected: "about"} %>`

3.

`<%= render partial: "nav", locals: {selected: "about"}`

Q 1 / 59

Ruby On Rails

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

1.

`skip_before_action :get_feature`

2.

`skip :get_feature, except: []`

3.

`prevent_action :get_feature`

4.

`:redis_cache_store`

Q 2 / 59

Ruby On Rails

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

1.

The `form_tag` method is for basic forms, while the `form_for` method is for multipart forms that include file uploads.

2.

The `form_tag` method is for HTTP requests, while the `form_for` method is for AJAX requests.

3.

The `form_tag` method typically expects a URL as its first argument, while the `form_for` method typically expects a model object.

4.

The `form_tag` method is evaluated at runtime, while the `form_for` method is precompiled and cached.

Q 3 / 59

Ruby On Rails

What is `before_action` (formerly known as `before_filter`)?

1.

A trigger that is executed before an alteration of an object's state

2.

A method that is executed before an ActiveRecord model is saved

3.

A callback that fires before an event is handled

4.

A method in a controller that is executed before the controller action method

Q 4 / 59

Ruby On Rails

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

1.

`ActiveSupport::Concern`

2.

`RailsHelper.CommonClass`

3.

`ActiveJob::Mixin`

4.

`ActiveSupport::Module`

Q 5 / 59

Ruby On Rails

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

1.

`put :items, include: patch`

2.

`put 'items', to: 'items#update'`

3.

`match 'items', to 'items#update', via: [:put, :patch]`

4.

`match :items, using: put && patch`

Q 6 / 59

Ruby On Rails

Which choice includes standard REST HTTP verbs?

1.

GET, POST, PATCH, DELETE

2.

REDIRECT, RENDER, SESSION, COOKIE

3.

INDEX, SHOW, NEW, CREATE, EDIT, UPDATE, DESTROY

4.

CREATE, READ, UPDATE, DELETE

Q 7 / 59

Ruby On Rails

Which ActiveRecord query prevents SQL injection?

1.

`Product.where("name = #{@keyword}")`

2.

`Product.where("name = " << @keyword}`

3.

`Product.where("name = ?", @keyword`

4.

`Product.where("name = " + h(@keyword)`

Q 8 / 59

Ruby On Rails

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

1.

It would include a column for `:type`.

2.

It would include columns for `:documentable_id` and `:documentable_type`.

3.

It would include columns for `:documentable` and `:type`.

4.

It would include a column for `:polymorphic_type`.

Q 9 / 59

Ruby On Rails

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

1.

Yes, any instance variables that are set in an action method on a controller can be accessed and displayed in a view.

2.

Yes, instance variables set within an action method are accessible within a view, but only when render is explicitly called inside the action method.

3.

No, instance variables in a controller are private and are not accessible.

4.

No, instance variables can never be set in a controller action method.

Q 10 / 59

Ruby On Rails

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

1.

`my_model.errors[:field]`

2.

`my_model.get_errors_for(:field)`

3.

`my_model.field.error`

4.

`my_model.all_errors.select(:field)`

Q 11 / 59

Ruby On Rails

If a database table of users contains the following rows, and `id` is the primary key, which statement would return only an object whose `last_name` is "Cordero"?

| id | first_name | last_name | |----|------------|-----------| | 1 | Alice | Anderson | | 2 | Bob | Buckner | | 3 | Carrie | Cordero | | 4 | Devon | Dupre | | 5 | Carrie | Eastman |

1.

undefined

2.

undefined

3.

`User.where(first_name: "Carrie")`

4.

`User.not.where(id: [1, 2, 4, 5])`

5.

`User.find_by(first_name: "Cordero")`

6.

`User.find(3)`

Q 12 / 59

Ruby On Rails

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

1.

`<%= select_tag(@products) %>`

2.

`<%= collection_select(@products) %>`

3.

`<select name="product_id"> <%= @products.each do |product| %> <option value="<%= product.id %>"/> <% end %></select>`

4.

`<%= collection_select(:product, :product_id, Product.all, :id, :name) %>`

Q 13 / 59

Ruby On Rails

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

1.

`model.errors = This address is invalid`

2.

`errors(model, :address) << "This address is invalid"`

3.

`display_error_for(model, :address, "This address is invalid")`

4.

`model.errors[:address

Q 14 / 59

Ruby On Rails

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

1.

If sent using the PATCH HTTP method, the URL could be used to update a product in the database.

2.

If sent using the POST HTTP method, the URL would create a new product in the database.

3.

If sent using the GET HTTP method, the URL would execute the show action in ProductsController.

4.

If sent using the DELETE HTTP method, the URL would call the destroy action by default.

Q 15 / 59

Ruby On Rails

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

1.

The user's documents will be loaded.

2.

The index action will run normally because `:index` is not listed as an argument to `before_action`.

3.

The `require_login` method will automatically log in the user before running the index action.

4.

The index action will not be run if the `require_login` method calls render or `redirect_to`.

Q 16 / 59

Ruby On Rails

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

1.

`render partial: ‘shared/menu’, cached: true`

2.

`render_with_cache partial: ‘shared/menu’`

3.

`render partial: ‘shared/menu’`

4.

`render partial: ‘shared/menu’, cached_with_variables: {}`

Q 17 / 59

Ruby On Rails

What is the reason for using Concerns in Rails?

1.

Concerns allow modularity and code reuse in models, controllers, and other classes.

2.

Concerns are used to separate class methods from models.

3.

Concerns are used to increase security of Rails applications.

4.

Concerns are used to refactor Rails views.

Q 18 / 59

Ruby On Rails

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

1.

`build`

2.

`new`

3.

`create`

4.

`save`

Q 19 / 59

Ruby On Rails

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?

1.

`CoffeeOrders`

2.

`Coffee_Orders`

3.

`Coffee_Order`

4.

`CoffeeOrder`

Q 20 / 59

Ruby On Rails

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

1.

The `has_many: through` association is the one-to-many equivalent to the `belongs_to` one-to-one association.

2.

Both associations are identical, and `has_many: through` is maintained only for legacy purposes.

3.

The `has_many` association is a one-to-many association, while `has_many: through` is a one-to-one association that matches through a third model.

4.

Both are one-to-many associations but with `has_many :through`, the declaring model can associate through a third model.

Q 21 / 59

Ruby On Rails

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

1.

Create an embedded Ruby file (.html.erb) and surround the Ruby code with `<% %>`.

2.

Insert Ruby code inside standard HTML files and surround it with `<% %>`. The web server will handle the rest.

3.

Create an embedded Ruby file (.html.erb) and surround the Ruby code with `<%= %>`.

4.

Put the code in an .rb file and include it in a `<link>` tag of an HTML file.

Q 22 / 59

Ruby On Rails

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

1.

`<% render 'view_mobile' %>`

2.

`<% render 'view', use_layout: 'mobile' %>`

3.

`<% render 'view', layout: 'mobile' %>`

4.

`<% render_with_layout 'view', 'mobile' %>`

Q 23 / 59

Ruby On Rails

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

1.

The product will not be updated and the edit template will be rendered.

2.

The product will not be updated and the controller will raise an ActiveModel::ForbiddenAttributes exception.

3.

The product will be updated with the values for name, style, and color, but the value for price will be ignored.

4.

The product will be updated with the values for name, style, color, and price.

Q 24 / 59

Ruby On Rails

A Rails project has ActiveRecord classes defined for Classroom and Student. If instances of these classes are related so that students are assigned the ID of one particular classroom, which choice shows the correct associations to define?

rb class Classroom < ActiveRecord::Base belongs_to :students, class_name: 'Student' end class Student < ActiveRecord::Base belongs_to :classrooms, class_name: 'Classroom' end rb class Student < ActiveRecord::Base has_many :classrooms, dependent: true end class Classroom < ActiveRecord::Base has_many :students, dependent: false end rb class Student < ActiveRecord::Base has_many :classrooms end class Classroom < ActiveRecord::Base belongs_to :student end rb class Classroom < ActiveRecord::Base has_many :students end class Student < ActiveRecord::Base belongs_to :classroom end

1.

A

2.

B

3.

C

4.

D

Q 25 / 59

Ruby On Rails

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

1.

app/static

2.

app/images

3.

app/assets

4.

app/views

Q 26 / 59

Ruby On Rails

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?

1.

`<script src="/main.js"></script>`

2.

`<%= javascript_include_tag 'main' %>`

3.

`<%= javascript_tag 'main' %>`

4.

`<!-- include_javascript 'main' -->`

Q 27 / 59

Ruby On Rails

In Rails, what caching stores can be used?

1.

MemCacheStore, MongoDBStore, MemoryStore, and FileStore

2.

MemoryStore, FileStore, and CacheCacheStore

3.

MemoryStore, FileStore, MemCacheStore, RedisCacheStore, and NullStore

4.

MemoryStore, FileStore, MySQLStore, and RedisCacheStore

Q 28 / 59

Ruby On Rails

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

1.

`rails generate controller --options {name: "Products", actions: "index"}`

2.

`rails generate controller --name Products --action index`

3.

`rails generate controller Products index`

4.

`rails generate ProductsController --actions index`

Q 29 / 59

Ruby On Rails

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

1.

`product_table`

2.

`all_products`

3.

`products_table`

4.

`products`

Q 30 / 59

Ruby On Rails

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

1.

Mustache

2.

Haml

3.

Liquid

4.

Tilt

Q 31 / 59

Ruby On Rails

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

1.

The method executes using "sudo" privileges.

2.

Any ending line return will be omitted from the result.

3.

The method will ignore exceptions that occur during execution.

4.

It is a more powerful or destructive version of the method.

Q 32 / 59

Ruby On Rails

What part of the code below causes the method `#decrypt_data` to be run?

rb class MyModel < ApplicationRecord after_find :decrypt_data end

1.

`MyModel.first.update(field: 'example')`

2.

`MyModel.where(id: 42)`

3.

`MyModel.first.destroy`

4.

`MyModel.new(field: 'new instance')`

Q 33 / 59

Ruby On Rails

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

1.

`csrf_protection`

2.

`csrf_helper`

3.

`csrf_meta_tags`

4.

`csrf`

Q 34 / 59

Ruby On Rails

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 }

1.

`encrypt_data`

2.

`clear_cache`

3.

`notify_admin_users`

4.

None of these callbacks will be called when `is_admin` is true.

Q 35 / 59

Ruby On Rails

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

1.

It filters out all parameters.

2.

It filters out submitted form parameters that are not named `:name` or `:sku` to make forms more secure.

3.

It raises an error if parameters that are not named `:name` or `:sku` are found.

4.

It raises an error if the `:name` and `:sku` parameters are set to `nil`.

Q 36 / 59

Ruby On Rails

Review the code below. Which Ruby operator should be used to fill in the blank so that the `sort` method executes properly?

rb

1.

`=>`

2.

`<==>`

3.

`<=>`

4.

`||`

Q 37 / 59

Ruby On Rails

Which ActiveRecord query prevents SQL injection?

1.

`Product.where("name = " << @keyword)`

2.

`Product.where("name = " + h(@keyword))`

3.

`Product.where("name = ?", @keyword)`

4.

`Product.where("name = #{@keyword}")`

Q 38 / 59

Ruby On Rails

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?

class IAmADummy < ActiveRecord::Migration def change rename_column :accounts, :account_hodler, :account_holder end end class FixSpellling < ActiveRecord::Migration def change rename :accounts, :account_hodler, :account_holder end end class CoffeeNeeded < ActiveRecord::Migration def change remove_column :accounts, :account_hodler add_column :accounts, :account_holder end end class OopsIDidItAgain < ActiveRecord::Migration def rename :accounts, :account_hodler, :account_holder end end

1.

A

2.

B

3.

C

4.

D

Q 39 / 59

Ruby On Rails

Which HTML is closes to what this code would output?

<% check_box(:post, :visible) %> <input type="hidden" name="post[visible]" value="0" /> <input type="checkbox" name="post[visible]" value="1" /> <checkbox name="post[visible]" value="1" /> <input type="checkbox" name="post[visible]" value="1" data-default-value="0" /> <input type="checkbox" name="post[visible]" value="1" />

1.

A

2.

B

3.

C

4.

D

Q 40 / 59

Ruby On Rails

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

1.

The string assigned to flash[:notice

2.

An instance variable should be used for flash[:notice]

3.

This is an invalid syntax to use to assign valuse to flash[:notice]

4.

The previous value of flash[:notice

Q 41 / 59

Ruby On Rails

Which statement about ActiveRecord models is true?

1.

Each database column requres adding a matching attr_accessor declaration in the ActiveRecord model.

2.

All attributes in an ActiveRecord model are read-only declared as writable using attr_accessible

3.

An instance of an ActiveRecord model will have attributes that match the columns in a corresponding database table.

4.

ActiveRecord models can have only attributes that have a matching database column

Q 42 / 59

Ruby On Rails

What is the correct way to assign a value to the session?

$_SESSION['user_id'] = user.id @session ||= Session.new << user.id session_save(:user_id, user.id) session[:user_id] = user.id

1.

A

2.

B

3.

C

4.

D

Q 43 / 59

Ruby On Rails

undefined

@result = Article.first.tags.build(name: 'Urgent')

1.

either true or false

2.

an unsaved Tag instance

3.

a saved Tag instance

4.

an array of Tag instances

Q 44 / 59

Ruby On Rails

undefined

<% render :head do %> <title>My page title</title> <% end %> <% content_for :head do %> <title>My page title</title> <% end %> <% render "shared/head, locals: {title: "My page title"} %> <% tield :head do %> <title>My page title</title> <% end %>

1.

A

2.

B

3.

C

4.

D

Q 45 / 59

Ruby On Rails

undefined

class Project validates :name, presence: true, length: { maximum: 50 }, uniqueness: true end class Project validate_attribute :name, [:presence, :uniqueness], :length => 1..50 end class Project validate_before_save :name, [:presence, [:length, 50], :uniqueness], :length => 1..50 end class Project validates_presense_of :name, :unique => true validates_length_of :name, :maximum => 50 end

1.

A

2.

B

3.

C

4.

D

Q 46 / 59

Ruby On Rails

undefined

class Product << ApplicationRecord ____ :photo end

1.

has_one_attached

2.

has_image

3.

attached_file

4.

acts_as_attachment

Q 47 / 59

Ruby On Rails

undefined

link_to('Link', {controller: 'products', action: 'index', page: 3}) /products?page=3 /products/index/3 /products/page/3 /products/index/page/3

1.

A

2.

B

3.

C

4.

D

Q 48 / 59

Ruby On Rails

undefined

1.

view

2.

controller

3.

ActiveRecord

4.

model

Q 49 / 59

Ruby On Rails

undefined

1.

User.where(last_name: 'Smith')

2.

User.find_or_create(last_name: 'Smith')

3.

User.find_by_last_name('Smith')

4.

User.find('Smith')

Q 50 / 59

Ruby On Rails

undefined

1.

route "products/index", to: "products/index", via: :get

2.

match "products/index", to: "products#index", via: :get

3.

root "products/index"

4.

get "products/index"

Q 51 / 59

Ruby On Rails

undefined

1.

BlogPost.joins (:comments).where(comments: {created_at: @range})

2.

BlogPost.where(['comments.created_at', @range])

3.

BlogPost.preload ("comments.created_at").where(created_at: @range)

4.

BlogPost.includes (:comments).where('comments.created_at' => @range)

Q 52 / 59

Ruby On Rails

undefined

ruby class Category < ActiveRecord::Base # has a database column for :name end category = Category.first category.name = 'News' saved_name = _____

1.

category.name_was

2.

category.saved(:name)

3.

category.changes[:name]

4.

category.name_changed?

Q 53 / 59

Ruby On Rails

undefined

ruby class LineItem < ApplicationRecord end class Order < ApplicationRecord has_many :line_items end Order.limit(3).each { |order| puts order.line_items }

1.

This query will result in extensive caching, and you will have to then deal with caching issues.

2.

This query will result in the N+1 query issue. Three orders will result in four queries.

3.

This query will result in the 1 query issue. Three orders will result in one query.

4.

There are no issues with this query, and you are correctly limiting the number of Order models that will be loaded.

Q 54 / 59

Ruby On Rails

undefined

1.

`<%= render(:partial => 'shared/product') %>`

2.

`<%= render('shared/product', :collection => @products) %>`

3.

`<%= render(template: 'shared/product', with: @products) %>`

4.

`<%= render('shared/product', locals: { product: @product }) %>`

Q 55 / 59

Ruby On Rails

undefined

1.

`before_action :login_required, skip: [:get_posts]`

2.

`skip_before_action :login_required, except: [:get_posts]`

3.

`skip_before_action :login_required, only: [:get_posts]`

4.

`skip_action before: :login_required, only: [:get_posts]`

Q 56 / 59

Ruby On Rails

undefined

ruby after_update_commit do destroy end ruby after_destroy do Rails.cache.delete(cache_key) end ruby after_update_commit do Rails.cache.delete(cache_key) end ruby after_update_commit do Rails.cache.destroy(cache_key) end

1.

A

2.

B

3.

C

4.

D

Q 57 / 59

Ruby On Rails

undefined

ruby 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

1.

The galleries table will have no primary key.

2.

The galleries table will include a column named "updated_at".

3.

The galleries table will contain exactly seven columns.

4.

The galleries table will have an index on the position column.

Q 58 / 59

Ruby On Rails

undefined

ruby class UsersController < ApplicationController def show @user = User.find(params[:id]) render json: @user, status: :ok, # Missing code end ruby rescue => e logger.info e end ruby rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response ruby rescue ActiveRecord::RecordNotFound render json: { message: 'User not found' }, status: :not_found end ruby raise ActiveRecord::RecordNotFound render json: { message: 'User not found' }, status: :user_not_found end

1.

A

2.

B

3.

C

4.

D

Q 59 / 59