Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Desenvolvimento Web com Ruby on Rails (parte 4)

Desenvolvimento Web com Ruby on Rails (parte 4)

Desenvolvimento Web com Ruby on Rails

More Decks by João Lucas Pereira de Santana

Other Decks in Education

Transcript

  1. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de

    Santana gtalk | linkedin | twitter: jlucasps
  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. 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. 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. 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. 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. 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} %>
  8. 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 %>
  9. 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 %>
  10. 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
  11. 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
  12. 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" %>
  13. 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
  14. 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
  15. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de

    Santana gtalk | linkedin | twitter: jlucasps Obrigado!