0
0
Ruby on Railsframework~10 mins

MVC architecture in Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Rails controller named PostsController.

Ruby on Rails
class PostsController < [1]
  def index
    @posts = Post.all
  end
end
Drag options to blanks, or click blank then click option'
AApplicationRecord
BApplicationController
CActiveRecord::Base
DActionController::Base
Attempts:
3 left
💡 Hint
Common Mistakes
Using ApplicationRecord which is for models.
Using ActiveRecord::Base which is for models.
Using ActionController::Base directly instead of ApplicationController.
2fill in blank
medium

Complete the code to define a model named Post that inherits from the correct Rails class.

Ruby on Rails
class Post < [1]
  validates :title, presence: true
end
Drag options to blanks, or click blank then click option'
AApplicationRecord
BActiveRecord::Base
CApplicationController
DActionController::Base
Attempts:
3 left
💡 Hint
Common Mistakes
Using ApplicationController which is for controllers.
Using ActionController::Base which is for controllers.
Using ActiveRecord::Base directly instead of ApplicationRecord.
3fill in blank
hard

Fix the error in the view code to correctly display all post titles.

Ruby on Rails
<ul>
  <% @posts.[1] do |post| %>
    <li><%= post.title %></li>
  <% end %>
</ul>
Drag options to blanks, or click blank then click option'
Aselect
Bmap
Ceach
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using map or collect which return new arrays but don't directly output.
Using select which filters but does not iterate all items.
4fill in blank
hard

Fill both blanks to complete the route that maps GET requests for posts to the index action.

Ruby on Rails
Rails.application.routes.draw do
  [1] '[2]', to: 'posts#index'
end
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cposts
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for a read route.
Using 'index' as the path instead of the resource name.
5fill in blank
hard

Fill all three blanks to complete a controller action that creates a new Post and redirects to its show page.

Ruby on Rails
def create
  @post = Post.new([1])
  if @post.[2]
    redirect_to [3], notice: 'Post was successfully created.'
  else
    render :new
  end
end
Drag options to blanks, or click blank then click option'
Apost_params
Bsave
C@post
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using update instead of save in create action.
Redirecting to a path string instead of the post object.
Passing raw params instead of permitted parameters.