index action?class ArticlesController < ApplicationController def index @articles = ['Ruby', 'Rails', 'MVC'] end end
Rails by default renders the view template matching the controller action name. Here, index will render app/views/articles/index.html.erb which can use @articles to display the list.
Rails.application.routes.draw do get 'welcome/index' root to: 'welcome#index' resources :articles get 'articles/show/:id' => 'articles#show' end
The route get 'articles/show/:id' is not a standard RESTful pattern and may cause confusion. The correct pattern is get 'articles/:id' which matches the show action properly.
@name = 'Alice', what will this ERB template output?<h1>Welcome, <%= @name.upcase %>!</h1> <p>Today is <%= Date.today.strftime('%A') %>.</p>
Date is accessible in views.The @name.upcase converts 'Alice' to 'ALICE'. The Date.today.strftime('%A') outputs the current weekday name, e.g., 'Monday'.
<%= form_with url: articles_path, method: :post do %>
<%= text_field_tag :title %>
<%= submit_tag 'Create' %>
<% end %>By default, form_with submits data via AJAX (remote: true), so the page does not reload. If the controller does not handle AJAX, it may seem like the form does nothing.
ApplicationController in a Rails app?ApplicationController in a Rails application.ApplicationController is the parent class for all controllers in Rails. It allows sharing methods, filters, and behavior across controllers.