Skip to content

Instantly share code, notes, and snippets.

@gbpereira
Last active May 15, 2017 21:39
Show Gist options
  • Save gbpereira/79a6f87c60add76c723afc4cc61274f2 to your computer and use it in GitHub Desktop.
Save gbpereira/79a6f87c60add76c723afc4cc61274f2 to your computer and use it in GitHub Desktop.
Comandos para auxilio do minicurso de introdução ao Ruby on Rails
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% @product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all.map { |c| [c.name, c.id] } %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
class Category < ActiveRecord::Base
has_many :products
validates :name, presence: true, uniqueness: true
end
require 'rails_helper'
RSpec.describe Category, type: :model do
describe 'validations' do
context 'name' do
it 'presence' do
category = FactoryGirl.build(:category, name: nil)
expect(category.valid?).to_not be_truthy
expect(category.errors.messages[:name].size).to eq(1)
end
it 'uniqueness' do
persisted_category = FactoryGirl.create(:category)
category = persisted_category.dup
expect(category.valid?).to_not be_truthy
expect(category.errors.messages[:name].size).to eq(1)
end
end
end
end

habilitando uso da rvm no terminal do gnome

Leia esse link caso seu terminal não reconheça os comandos da rvm

pra instalar a rvm

Script ou Comandos

clonando o template

git clone [email protected]:gbpereira/rails-template.git seccomp-app

selecionando a versão do ruby e o gemset do projeto

rvm use 2.2.3@seccomp

criando o crud básico de Categorias

rails g scaffold category name:string

criando o model de Produtos

rails g model product name:string description:text category:references

criando o controller de Produtos

rails g controller products --skip-assets --no-helper

rodando as migrations pendentes

rake db:migrate (desenvolvimento) rake db:migrate RAILS_ENV=test (teste)

<p id="notice"><%= notice %></p>
<h1>Listing Products</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Category</th>
<th></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.category_name %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Product', new_product_path %>
class Product < ActiveRecord::Base
belongs_to :category
delegate :name, to: :category, allow_nil: true, prefix: true
end
class ProductsController < ApplicationController
before_action :set_product, only: [:edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
end
def edit
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to products_url, notice: 'Product was successfully created.'
else
render :new
end
end
def update
if @product.update(product_params)
redirect_to products_url, notice: 'Product was successfully updated.'
else
render :edit
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :description, :category_id)
end
end
Rails.application.routes.draw do
resources :categories
resources :products, except: :destroy
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment