0
0
Ruby on Railsframework~10 mins

Action methods in Ruby on 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 an action method named show in a Rails controller.

Ruby on Rails
class ProductsController < ApplicationController
  def [1]
    # code to show a product
  end
end
Drag options to blanks, or click blank then click option'
Ashow
Bdisplay
Cindex
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-standard method name like display.
Confusing show with index which lists all items.
2fill in blank
medium

Complete the code to render the index view in the index action method.

Ruby on Rails
class ArticlesController < ApplicationController
  def index
    @articles = Article.all
    [1]
  end
end
Drag options to blanks, or click blank then click option'
Arender 'show'
Bredirect_to articles_path
Credirect_to index
Drender :index
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect_to instead of render when showing a view.
Rendering the wrong template like 'show'.
3fill in blank
hard

Fix the error in the create action by completing the code to save a new Post.

Ruby on Rails
class PostsController < ApplicationController
  def create
    @post = Post.new(post_params)
    if @post.[1]
      redirect_to @post
    else
      render :new
    end
  end
end
Drag options to blanks, or click blank then click option'
Aupdate
Bsave!
Csave
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using save! which raises errors instead of returning false.
Using create which is a class method, not an instance method.
4fill in blank
hard

Fill both blanks to define a destroy action that finds a User by id and deletes it.

Ruby on Rails
class UsersController < ApplicationController
  def destroy
    @user = User.[1](params[:[2]])
    @user.destroy
    redirect_to users_path
  end
end
Drag options to blanks, or click blank then click option'
Afind
Bid
Cfind_by
Duser_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using find_by without specifying the correct key.
Using user_id instead of id in params.
5fill in blank
hard

Fill all three blanks to define an update action that finds a Comment, updates it with permitted params, and redirects to the comment.

Ruby on Rails
class CommentsController < ApplicationController
  def update
    @comment = Comment.[1](params[:[2]])
    if @comment.[3](comment_params)
      redirect_to @comment
    else
      render :edit
    end
  end
end
Drag options to blanks, or click blank then click option'
Afind
Bid
Cupdate
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of update which requires manual attribute assignment.
Using wrong param key like comment_id instead of id.