Ruby on Rails Snippets for VS Code
⚠️ Disclaimer ⚠️
This is not the official Visual Studio Code extension. This is a community-created extension with
a comprehensive collection of snippets for Ruby on Rails development in Visual Studio Code. This extension provides snippets for all major Rails components and common patterns.
Features
- Comprehensive collection of Rails snippets with standardized naming (r-prefix)
- Support for both Ruby and ERB files
- Tab stops for easy navigation and customization
- Basic and Complete versions of major components
- Modern UI components with Bootstrap styling
- API development support
- Testing templates for Minitest, RSpec, and System Tests
- Form handling and validation
- Database migrations and models
- View helpers and partials
- Service objects and concerns
Snippets Included
Models & Database
rmodel
- Basic model template
rmodelc
- Complete model template with associations, validations, callbacks, and scopes
rmigration
- Migration template for creating tables
rmigrationadd
- Migration template for adding columns
Controllers
rcontroller
- Basic controller template with index and show actions
rcontrollerc
- Complete controller template with all CRUD actions
rapicontroller
- API controller template with JSON responses and error handling
Views & UI Components
rform
- Form template with error handling
rpartial
- View partial template
rtable
- Table template with actions and Bootstrap styling
rcard
- Card template with Bootstrap styling
ralert
- Alert template with Bootstrap styling
rpagination
- Pagination template
rsearch
- Search form template
rfilter
- Filter form template
Routes
rroute
- Basic route definition
rroutec
- Complete route definition with nested resources
Testing
rtestm
- Model test template (Minitest)
rtestc
- Controller test template (Minitest)
rsystest
- System test template (Minitest)
rspecm
- RSpec model test template
rspecc
- RSpec controller test template
rspecr
- RSpec request test template
rfactory
- FactoryBot factory definition
Other Rails Components
rconcern
- Concern template
rjob
- Active Job template
rmailer
- Mailer template
rhelper
- Helper module template
rservice
- Service object template
rdecorator
- Decorator template
Usage
- Type the snippet prefix (e.g.,
rmodel
) and press Tab
or Enter
- Use
Tab
to navigate between placeholders
- Customize the generated code as needed
Examples
Creating an API Controller
Type rapicontroller
and press Tab
:
class Api::V1::UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
def index
@users = User.all
render json: @users, status: :ok
end
def show
render json: @user, status: :ok
end
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created
else
render json: @user.errors, status: :unprocessable_entity
end
end
# ... other actions ...
private
def set_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
render json: { error: 'User not found' }, status: :not_found
end
def user_params
params.require(:user).permit(:name, :email)
end
end
Creating a View Table
Type rtable
and press Tab
:
<table class="table">
<thead>
<tr>
<% @users.first.attributes.keys.each do |attribute| %>
<th><%= attribute.titleize %></th>
<% end %>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<% user.attributes.values.each do |value| %>
<td><%= value %></td>
<% end %>
<td>
<%= link_to 'Show', user, class: 'btn btn-info btn-sm' %>
<%= link_to 'Edit', edit_user_path(user), class: 'btn btn-warning btn-sm' %>
<%= button_to 'Delete', user, method: :delete, class: 'btn btn-danger btn-sm', data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</tbody>
</table>
Creating a System Test
Type rsystest
and press Tab
:
require 'application_system_test_case'
class UserManagementTest < ApplicationSystemTestCase
setup do
@user = User.create!(email: 'test@example.com', password: 'password123')
end
test "visiting the users index" do
visit users_url
assert_selector "h1", text: "Users"
end
test "creating a new user" do
visit new_user_url
fill_in "Email", with: "new@example.com"
fill_in "Password", with: "password123"
click_on "Create User"
assert_text "User was successfully created"
end
end
Type rsearch
and press Tab
:
<%= form_with url: search_users_path, method: :get, class: "mb-4" do |f| %>
<div class="input-group">
<%= f.text_field :q, value: params[:q], class: "form-control", placeholder: "Search users..." %>
<%= f.select :role, User.roles.keys, { include_blank: true }, class: "form-select" %>
<%= f.submit "Search", class: "btn btn-primary" %>
</div>
<% end %>
Contributing
We welcome your feedback and suggestions to improve these snippets. Please feel free to:
- Report bugs
- Suggest new snippets
- Improve existing snippets
- Update documentation
- Share the extension with others
License
This extension is licensed under the MIT License - see the LICENSE file for details.
Support
If you find this extension helpful, please consider:
- Sharing the extension with your network
- Reporting bugs or suggesting improvements
- Giving a rating on the VS Code Marketplace
Acknowledgments
- Thanks to the VS Code team for their extensible editor & excellent extension API
- Thanks to the Rails community for their continuous support and feedback
- Thanks to all contributors who have helped improve this extension