Framework "Ruby on Rails" a web 2.0 (Prílohy)
Skočit na navigaci
Skočit na vyhledávání
1. | Fenomén Web 2.0 |
2. | Ruby on Rails – vývoj dynamických web 2.0 aplikácií |
3. | Framework "Ruby on Rails" a web 2.0 (Prílohy)
|
Obsah
Prílohy
Príloha A
controllers/books_controller.rb
class BooksController < ApplicationController
# GET /books
# GET /books.xml
def index
@books = Book.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @books }
end
end
# GET /books/1
# GET /books/1.xml
def show
@book = Book.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/new
# GET /books/new.xml
def new
@book = Book.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/1/edit
def edit
@book = Book.find(params[:id])
end
# POST /books
# POST /books.xml
def create
@book = Book.new(params[:book])
respond_to do |format|
if @book.save
flash[:notice] = 'Book was successfully created.'
format.html { redirect_to(@book) }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# PUT /books/1
# PUT /books/1.xml
def update
@book = Book.find(params[:id])
respond_to do |format|
if @book.update_attributes(params[:book])
flash[:notice] = 'Book was successfully updated.'
format.html { redirect_to(@book) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /books/1
# DELETE /books/1.xml
def destroy
@book = Book.find(params[:id])
@book.destroy
respond_to do |format|
format.html { redirect_to(books_url) }
format.xml { head :ok }
end
end
end
models/book.rb
class Book < ActiveRecord::Base
end
views/books/edit.html.erb
<h1>Editing book</h1>
<% form_for(@book) do |f| %>
<%= f.error_messages %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', @book %> |
<%= link_to 'Back', books_path %>
views/books/index.html.erb
<h1>Listing books</h1>
<table>
<tr>
</tr>
<% @books.each do |book| %>
<tr>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New book', new_book_path %>
views/books/new.html.erb
<h1>New book</h1>
<% form_for(@book) do |f| %>
<%= f.error_messages %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', books_path %>
views/books/edist.html.erb
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %