Ruby Rails Syntax Guide
Ruby Rails Syntax Guide
1. Model Declaration
2. Migrations
3. Routes
Rails.application.routes.draw do
resources :posts
root "posts#index"
end
4. Controller Actions
if @post.save
redirect_to @post
else
render :new
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
6. Form Helpers
7. Model Validations
8. Associations
end
9. ActiveRecord Queries
Post.all
Post.find(1)
Post.where(title: 'Hello')
Post.order(:created_at)
Post.first
In view:
<% if notice %>
<p><%= notice %></p>
<% end %>
11. Partials
File: _form.html.erb
12. Helpers
module PostsHelper
def format_date(date)
date.strftime("%B %d, %Y")
end
end
Used in view:
<p><%= format_date(@post.created_at) %></p>
self.title = title.capitalize
end
end
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
</head>
<body>
<%= yield %>
</body>
</html>