Desenvolvimento
Web com Ruby on
Rails
João Lucas Pereira de Santana
gtalk | linkedin | twitter: jlucasps
Resources
Resources são objetos que os usuários estão aptos a
acessar e realizar operações CRUD (ou um
conjunto delas)
Con...
Resources
@jlucasps
class MessagesController < ActionController::Base
# GET messages_url
def index
# return all messages
e...
Resources
@jlucasps
messages GET /messages(.:format)
messages#index
POST /messages(.:format)
messages#create
new_message G...
Resources
Alterar tela index.html.erb para conter link
para listagem de usuários
@jlucasps
<div class="span9">
<% label = ...
Resources
Criar tela de listagem de usuários em
/app/views/users/index.html.erb
@jlucasps
<% if @users.any? %>
<% # Listag...
Resources
Criar controller de usuários em
/app/controllers/users_controller.rb
@jlucasps
class UsersController < Applicati...
Resources
Tela de listagem de usuários
@jlucasps
Criar a action new para exibir formulário
Resources
@jlucasps
class UsersController < ApplicationController
def index
@use...
Resources
@jlucasps
/app/views/shared/_error_messages.html.erb
<% if resource.errors.any? %>
<div id="error_explanation">
...
Resources
Partial com formulário em /app/views/users/_form.html.erb
@jlucasps
<%= form_for(user) do |f| %>
<%= render :par...
Resources
Formulário de novo usuário
@jlucasps
Resources
Implementar action create
@jlucasps
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "U...
Resources
Implementar a action e view show
@jlucasps
<p id="notice"><%= notice %></p>
<p><b>Nome:</b><%= @user.name %></p>...
Resources
Tela de exibição de usuários
@jlucasps
Resources
Completar tela de listagem
@jlucasps
<% if @users.any? %>
<table class="table table-bordered">
<% @users.each do...
Resources
@jlucasps
Resources
Implementar actions de edit e update
@jlucasps
<h4>Editar usuário</h4>
<%= render :partial => 'form', :locals =>...
Resources
Action destroy
@jlucasps
def destroy
@user = User.find(params[:id])
flash[:notice] = (@user.destroy ? "Usuario d...
Resources
Listagem final de usuários
@jlucasps
Desenvolvimento
Web com Ruby on
Rails
João Lucas Pereira de Santana
gtalk | linkedin | twitter: jlucasps
Obrigado!
Upcoming SlideShare
Loading in...5
×

Desenvolvimento web com Ruby on Rails (parte 4)

330

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
330
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Desenvolvimento web com Ruby on Rails (parte 4)"

  1. 1. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps
  2. 2. Resources Resources são objetos que os usuários estão aptos a acessar e realizar operações CRUD (ou um conjunto delas) Controllers de objetos Resources são implementados utilizando-se os métodos (GET, PUT, POST) defindidos no protocolo HTTP @jlucasps resources :messages namespace "admin" do resources :posts, :comments # app/controllers/admin/posts end resources :magazines do resources :ads end
  3. 3. Resources @jlucasps class MessagesController < ActionController::Base # GET messages_url def index # return all messages end # GET new_message_url def new # return an HTML form for describing a new message end # POST messages_url def create # create a new message end # GET message_url(:id => 1) def show # find and return a specific message end # GET edit_message_url(:id => 1) def edit # return an HTML form for editing a specific message end # PUT message_url(:id => 1) def update # find and update a specific message end # DELETE message_url(:id => 1) def destroy # delete a specific message end end
  4. 4. Resources @jlucasps messages GET /messages(.:format) messages#index POST /messages(.:format) messages#create new_message GET /messages/new(.:format) messages#new edit_message GET /messages/:id/edit(.:format) messages#edit message GET /messages/:id(.:format) messages#show PUT /messages/:id(.:format) messages#update DELETE /messages/:id(.:format) messages#destroy
  5. 5. Resources Alterar tela index.html.erb para conter link para listagem de usuários @jlucasps <div class="span9"> <% label = "<i class='icon-user'></i>&nbsp;Usuários". html_safe %> <%= link_to label, users_path, :class => "btn btn-large" % > </div><!--/span--> <%= content_for :sidebar do %> <%= render :partial => 'shared/sidebar' %> <% end %>
  6. 6. Resources Criar tela de listagem de usuários em /app/views/users/index.html.erb @jlucasps <% if @users.any? %> <% # Listagem de usuários %> <% else %> <div class="alert"> Nenhum usuário cadastrado </div> <% end %> <%= link_to "Novo usuário", new_user_path, :class => "btn btn-success" %>
  7. 7. Resources Criar controller de usuários em /app/controllers/users_controller.rb @jlucasps class UsersController < ApplicationController def index @users = User.all end end
  8. 8. Resources Tela de listagem de usuários @jlucasps
  9. 9. Criar a action new para exibir formulário Resources @jlucasps class UsersController < ApplicationController def index @users = User.all end def new @user = User.new end end <h4>Novo usuário</h4> <%= render :partial => 'form', :locals => {:user => @user} %>
  10. 10. Resources @jlucasps /app/views/shared/_error_messages.html.erb <% if resource.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(resource.errors.count, "error") %> erros:</h2> <ul> <% resource.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
  11. 11. Resources Partial com formulário em /app/views/users/_form.html.erb @jlucasps <%= form_for(user) do |f| %> <%= render :partial => 'shared/error_messages', :locals => {:resource => user} % > <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.text_field :email %> </div> <div class="field"> <%= f.label :age %><br /> <%= f.number_field :age %> </div> <div class="field"> <%= f.label :gender %><br /> <%= f.number_field :gender %> </div> <div class="actions"> <%= f.submit :class => "btn btn-primary" %> <%= link_to "Voltar", users_path, :class => "btn" %> </div> <% end %>
  12. 12. Resources Formulário de novo usuário @jlucasps
  13. 13. Resources Implementar action create @jlucasps def create @user = User.new(params[:user]) if @user.save flash[:notice] = "Usuário criado" redirect_to user_path(@user) else render :action => :new end end
  14. 14. Resources Implementar a action e view show @jlucasps <p id="notice"><%= notice %></p> <p><b>Nome:</b><%= @user.name %></p> <p><b>email:</b><%= @user.email %></p> <p><b>Idade:</b><%= @user.age %></p> <p><b>Sexo:</b><%= @user.gender %></p> <%= link_to 'Edit', edit_user_path(@user), :class => "btn" %> | <%= link_to 'Back', users_path, :class => "btn" %> def show @user = User.find(params[:id]) end
  15. 15. Resources Tela de exibição de usuários @jlucasps
  16. 16. Resources Completar tela de listagem @jlucasps <% if @users.any? %> <table class="table table-bordered"> <% @users.each do |user| %> <tr> <td> <%= "#{user.name} (#{user.email}), #{user.age} anos" %> <%= link_to "<i class='icon-edit'></i>".html_safe, edit_user_path(user), :class => "btn btn-mini" %> <%= link_to "<i class='icon-trash'></i>".html_safe, user, :method => :delete, : class => "btn btn-mini" %> </td> </tr> <% end %> </table> <% else %> <div class="alert"> Nenhum usuário cadastrado </div> <% end %> <%= link_to "Novo usuário", new_user_path, :class => "btn btn-success" %>
  17. 17. Resources @jlucasps
  18. 18. Resources Implementar actions de edit e update @jlucasps <h4>Editar usuário</h4> <%= render :partial => 'form', :locals => {:user => @user} %> /app/views/users/edit.html.erb def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) flash[:notice] = "Usuario modificado" redirect_to user_path(@user) else render :action => :edit end end
  19. 19. Resources Action destroy @jlucasps def destroy @user = User.find(params[:id]) flash[:notice] = (@user.destroy ? "Usuario deletado" : "Falha na remocao") redirect_to users_path end
  20. 20. Resources Listagem final de usuários @jlucasps
  21. 21. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps Obrigado!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×