SlideShare a Scribd company logo
1 of 21
Download to read offline
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)
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
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
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
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 %>
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" %>
Resources
Criar controller de usuários em
/app/controllers/users_controller.rb
@jlucasps
class UsersController < ApplicationController
def index
@users = User.all
end
end
Resources
Tela de listagem de usuários
@jlucasps
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} %>
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 %>
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 %>
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] = "Usuário criado"
redirect_to user_path(@user)
else
render :action => :new
end
end
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
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 |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" %>
Resources
@jlucasps
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
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
Resources
Listagem final de usuários
@jlucasps
Desenvolvimento
Web com Ruby on
Rails
João Lucas Pereira de Santana
gtalk | linkedin | twitter: jlucasps
Obrigado!

More Related Content

What's hot

Organize directories for applications with front-end and back-end with yii - ...
Organize directories for applications with front-end and back-end with yii - ...Organize directories for applications with front-end and back-end with yii - ...
Organize directories for applications with front-end and back-end with yii - ...Framgia Vietnam
 
Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)Joao Lucas Santana
 
ui-router and $state
ui-router and $stateui-router and $state
ui-router and $stategarbles
 
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...Nguyen Duc Phu
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Sumy PHP User Grpoup
 
Prefixルーティングとthemeのススメ
PrefixルーティングとthemeのススメPrefixルーティングとthemeのススメ
PrefixルーティングとthemeのススメShusuke Otomo
 
Functional UI (Cocoaheads Sydney, Sep 2015)
Functional UI  (Cocoaheads Sydney, Sep 2015)Functional UI  (Cocoaheads Sydney, Sep 2015)
Functional UI (Cocoaheads Sydney, Sep 2015)Robert J Chatfield
 
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編Masakuni Kato
 
Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNFacebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNStephan Hochdörfer
 
Build Secure Cloud-Hosted Apps for SharePoint 2013
Build Secure Cloud-Hosted Apps for SharePoint 2013Build Secure Cloud-Hosted Apps for SharePoint 2013
Build Secure Cloud-Hosted Apps for SharePoint 2013Danny Jessee
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Sumy PHP User Grpoup
 
Endpoint node.js framework presentation
Endpoint node.js framework presentationEndpoint node.js framework presentation
Endpoint node.js framework presentationabresas
 
Bullseye coverageerror
Bullseye coverageerrorBullseye coverageerror
Bullseye coverageerrorDutch Mill
 
Bullseye coverageerror
Bullseye coverageerrorBullseye coverageerror
Bullseye coverageerrorEder Alves
 
Restap ito uploadfilessharepoint
Restap ito uploadfilessharepointRestap ito uploadfilessharepoint
Restap ito uploadfilessharepointMAHESH NEELANNAVAR
 

What's hot (17)

Organize directories for applications with front-end and back-end with yii - ...
Organize directories for applications with front-end and back-end with yii - ...Organize directories for applications with front-end and back-end with yii - ...
Organize directories for applications with front-end and back-end with yii - ...
 
Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)Desenvolvimento web com Ruby on Rails (parte 6)
Desenvolvimento web com Ruby on Rails (parte 6)
 
ui-router and $state
ui-router and $stateui-router and $state
ui-router and $state
 
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
 
Prefixルーティングとthemeのススメ
PrefixルーティングとthemeのススメPrefixルーティングとthemeのススメ
Prefixルーティングとthemeのススメ
 
Functional UI (Cocoaheads Sydney, Sep 2015)
Functional UI  (Cocoaheads Sydney, Sep 2015)Functional UI  (Cocoaheads Sydney, Sep 2015)
Functional UI (Cocoaheads Sydney, Sep 2015)
 
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
 
Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNFacebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
 
Build Secure Cloud-Hosted Apps for SharePoint 2013
Build Secure Cloud-Hosted Apps for SharePoint 2013Build Secure Cloud-Hosted Apps for SharePoint 2013
Build Secure Cloud-Hosted Apps for SharePoint 2013
 
22.sessions in laravel
22.sessions in laravel22.sessions in laravel
22.sessions in laravel
 
Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2Service approach for development REST API in Symfony2
Service approach for development REST API in Symfony2
 
History frame
History frameHistory frame
History frame
 
Endpoint node.js framework presentation
Endpoint node.js framework presentationEndpoint node.js framework presentation
Endpoint node.js framework presentation
 
Bullseye coverageerror
Bullseye coverageerrorBullseye coverageerror
Bullseye coverageerror
 
Bullseye coverageerror
Bullseye coverageerrorBullseye coverageerror
Bullseye coverageerror
 
Restap ito uploadfilessharepoint
Restap ito uploadfilessharepointRestap ito uploadfilessharepoint
Restap ito uploadfilessharepoint
 

Similar to Desenvolvimento web com Ruby on Rails (parte 4)

Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxWen-Tien Chang
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)lazyatom
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Rails Routing and URL design
Rails Routing and URL designRails Routing and URL design
Rails Routing and URL designhiq5
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful RailsViget Labs
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful RailsBen Scofield
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2RORLAB
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4shnikola
 
Simple restfull app_s
Simple restfull app_sSimple restfull app_s
Simple restfull app_snetwix
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
Boston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsBoston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsJohn Brunswick
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slidesCao Van An
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Michelangelo van Dam
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Joao Lucas Santana
 

Similar to Desenvolvimento web com Ruby on Rails (parte 4) (20)

Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 Ajax
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Rails Routing and URL design
Rails Routing and URL designRails Routing and URL design
Rails Routing and URL design
 
18.register login
18.register login18.register login
18.register login
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
 
Simple restfull app_s
Simple restfull app_sSimple restfull app_s
Simple restfull app_s
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
Boston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on RailsBoston Computing Review - Ruby on Rails
Boston Computing Review - Ruby on Rails
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slides
 
Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8Unit testing after Zend Framework 1.8
Unit testing after Zend Framework 1.8
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 

More from Joao Lucas Santana

Critical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidadeCritical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidadeJoao Lucas Santana
 
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Joao Lucas Santana
 
Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)Joao Lucas Santana
 
Desenvolvimento web com Ruby on Rails (parte 5)
Desenvolvimento web com Ruby on Rails (parte 5)Desenvolvimento web com Ruby on Rails (parte 5)
Desenvolvimento web com Ruby on Rails (parte 5)Joao Lucas Santana
 
Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)Joao Lucas Santana
 
Desenvolvimento web com Ruby on Rails (parte 1)
Desenvolvimento web com Ruby on Rails (parte 1)Desenvolvimento web com Ruby on Rails (parte 1)
Desenvolvimento web com Ruby on Rails (parte 1)Joao Lucas Santana
 

More from Joao Lucas Santana (6)

Critical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidadeCritical Rendering Path - Velocidade também é uma funcionalidade
Critical Rendering Path - Velocidade também é uma funcionalidade
 
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
Um roadmap do Framework Ruby on Rails, do Rails 1 ao Rails 4 - DevDay 2013
 
Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)Desenvolvimento web com Ruby on Rails (extras)
Desenvolvimento web com Ruby on Rails (extras)
 
Desenvolvimento web com Ruby on Rails (parte 5)
Desenvolvimento web com Ruby on Rails (parte 5)Desenvolvimento web com Ruby on Rails (parte 5)
Desenvolvimento web com Ruby on Rails (parte 5)
 
Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)Desenvolvimento web com Ruby on Rails (parte 3)
Desenvolvimento web com Ruby on Rails (parte 3)
 
Desenvolvimento web com Ruby on Rails (parte 1)
Desenvolvimento web com Ruby on Rails (parte 1)Desenvolvimento web com Ruby on Rails (parte 1)
Desenvolvimento web com Ruby on Rails (parte 1)
 

Recently uploaded

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

Desenvolvimento web com Ruby on Rails (parte 4)

  • 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. Resources Criar controller de usuários em /app/controllers/users_controller.rb @jlucasps class UsersController < ApplicationController def index @users = User.all end end
  • 8. Resources Tela de listagem de usuários @jlucasps
  • 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. 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. 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. Resources Formulário de novo usuário @jlucasps
  • 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. 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. Resources Tela de exibição de usuários @jlucasps
  • 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" %>
  • 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. 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. Resources Listagem final de usuários @jlucasps
  • 21. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de Santana gtalk | linkedin | twitter: jlucasps Obrigado!