Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Ruby on Rails MVC & Full-Stack Development 18 mins read Free Lesson

RESTful Routing, Action Controller & Strong Parameters

Define resource routes in config/routes.rb and sanitize inputs with params.require().permit().

RESTful Routing & Strong Parameters

# app/controllers/tutorials_controller.rb
class TutorialsController < ApplicationController
  def index
    @tutorials = Tutorial.where(is_free: true)
  end

  def create
    @tutorial = Tutorial.new(tutorial_params)
    if @tutorial.save
      redirect_to @tutorial, notice: 'Tutorial created successfully!'
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

  def tutorial_params
    params.require(:tutorial).permit(:title, :content, :is_free)
  end
end