Form-based Workflow
This guide covers setting up the new action, building the form with form_for, handling validation errors, and connecting it all to your model.
Introduction
While we've created articles from the console, users need a web interface to add content. This document covers creating HTML forms and backend processing to submit articles.
Creating the NEW Action
Route Configuration
The RESTful route resources :articles sets up /articles/new for creating articles.
Controller Setup
Add to app/controllers/articles_controller.rb:
def new
@article = Article.new
endView Template
Create app/views/articles/new.html.erb:
<h1>Create a New Article</h1>Building the Form
Form Implementation
Add to new.html.erb:
<%= form_for(@article) do |f| %>
<ul>
<% @article.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>Form Components
form_for: Creates form for@articlef.label: Creates HTML labelf.text_field: Single-line text inputf.text_area: Multi-line text inputf.submit: Submit button
Processing Form Data
Create Action
Add to controller:
def create
@article = Article.new(article_params)
@article.save
redirect_to article_path(@article)
endStrong Parameters
Add private method for security:
private
def article_params
params.require(:article).permit(:title, :body)
endDeleting Articles
Delete Link
Add to show.html.erb:
<%= link_to "delete", article_path(@article), method: :delete, data: { confirm: "Really delete the article?" } %>Destroy Action
Add to controller:
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
endEditing Articles
Edit Workflow
Similar to new/create pattern:
editdisplays formupdateprocesses changes
Edit Link
Add to show.html.erb:
<%= link_to "edit", edit_article_path(@article) %>Edit Action
Add to controller:
def edit
@article = Article.find(params[:id])
endEdit Template
Create app/views/articles/edit.html.erb:
<h1>Edit an Article</h1>
<%= render partial: 'form' %>Form Partial
Create app/views/articles/_form.html.erb with shared form code.
Update Action
Add to controller:
def update
@article = Article.find(params[:id])
@article.update(article_params)
redirect_to article_path(@article)
endFlash Messages
Adding Messages
Example for update action:
flash.notice = "Article '#{@article.title}' Updated!"Displaying Messages
Add to app/views/layouts/application.html.erb:
<p class="flash"><%= flash.notice %></p>Setting Root Route
In config/routes.rb:
root to: 'articles#index'Git Workflow
Committing Changes
git add -A
git commit -m "form-based workflow feature completed"
git pushResetting Changes
To roll back to first commit:
git log
git reset --hard [commit-hash]Key Concepts
- Form Helpers: Rails provides helpers like
form_forto generate HTML forms - Strong Parameters: Security measure to whitelist form parameters
- RESTful Routes: Standardized URL patterns for CRUD operations
- DRY Views: Using partials to avoid code duplication
- User Feedback: Flash messages for status updates