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
end
View 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@article
f.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)
end
Strong Parameters
Add private method for security:
private
def article_params
params.require(:article).permit(:title, :body)
end
Deleting 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
end
Editing Articles
Edit Workflow
Similar to new/create pattern:
edit
displays formupdate
processes 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])
end
Edit 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)
end
Flash 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 push
Resetting Changes
To roll back to first commit:
git log
git reset --hard [commit-hash]
Key Concepts
- Form Helpers: Rails provides helpers like
form_for
to 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
Up and Running
Learn how to quickly install Rails, generate your first app, configure the database, and launch your local server
Adding Comments
This guide walks you through creating a Comment model, setting up one-to-many associations with articles, building a comment form, handling form submissions, displaying comments, and adding timestamps using Rails best practices.