Skip to content

Instantly share code, notes, and snippets.

@norbajunior
Forked from danielvlopes/roteiro-aula5.md
Created December 7, 2011 14:26
Show Gist options
  • Save norbajunior/1442994 to your computer and use it in GitHub Desktop.
Save norbajunior/1442994 to your computer and use it in GitHub Desktop.
Roteiro aula 5 - Una Web 2

Código fonte base:

http://daniellopes.s3.amazonaws.com/UNA/turma2/resume.zip

Adicionar ao Gemfile: gem 'css-bootstrap-rails'

Rodar no terminal

bundle install

Copiar o gist abaixo e colar no application.css

https://gist.github.com/1393504

Ajustar application.html.erb:

https://gist.github.com/1393504

Criar os helpers abaixo:

def page_header(title)
  content_tag :div, :class => "page-header" do
    content_tag :h1, title
  end
end

def error_messages_for(resource)
 render 'shared/error_messages', resource: resource
end

def flash_messages
 msgs = flash.collect { |key, msg| content_tag(:p, msg, class: key) }.join
 content_tag :div, msgs.html_safe, id: "flashMessage"
end

** Opcional: Ajustar todos os arquivos views/employments/index.html.erb e views/skills/index.html.erb:

<%= page_header "Listing" %>

** Opcional: Ajustar todos os arquivos views/employments/index.html.erb e views/skills/index.html.erb:

<table class="zebra-striped bordered-table">

** Opcional: Acrescentar o os parametros :html => { :class => "form-stacked"} em todos os forms. Por exemplo:

<%= form_for(@employment, :html => { :class => "form-stacked"})  do |f| %>

Criar o arquivo para renderização dos erros. Dentro da pasta views crie uma nova chamada shared. Em seguida crie um arquivo chamado _error_messages.html.erb com o conteúdo:

<% if resource.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(resource.errors.count, "error") %> prohibited this record from being saved:</h2>

    <ul>
    <% resource.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

Criar a pasta lib/templates/rails/scaffold_controller/ e dentro o arquivo controller.rb com conteúdo:

https://gist.github.com/1393504

No terminal rodar os comandos:

rails generate scaffold Skill name:string experience:string profile:references
rake db:migrate

Abrir o console do Rails

rails c --sandbox

Profile.create(first_name:"Daniel", last_name:"Lopes", job_title:"Dev", email:"[email protected]")  
p = Profile.first
p.skills
Skill.all
Skill.create(name:"Rails", experience:"Expert", profile: p)
p.skills
p.skills.reload
p.skills.count
Skills.first.profile
Skill.first.profile
Profile.first.skills.where(experience: "Expert")
Profile.first.skills.where(experience: "Expert").order(:name)

No controller de skills acrescentar:

before_filter :load_profiles

E também

private

def load_profiles
  @profiles = Profile.all
end

No form de skills alterar profile para:

<div class="field">
  <%= f.label :profile_id, "Profile" %>
  <%= f.collection_select :profile_id, @profiles, :id, :full_name %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment