rails destroy model book
rake db:drop
rails g scaffold book isbn:string title:string price:integer publish:string published:date cd:boolean
rake db:migrate
http://localhost:3000/books
resources :books
C:\user\dev\railbook>rake routes
books GET /books(.:format) {:action=>"index", :controller=>"books"}
POST /books(.:format) {:action=>"create", :controller=>"books"}
new_book GET /books/new(.:format) {:action=>"new", :controller=>"books"}
edit_book GET /books/:id/edit(.:format) {:action=>"edit", :controller=>"books"}
book GET /books/:id(.:format) {:action=>"show", :controller=>"books"}
PUT /books/:id(.:format) {:action=>"update", :controller=>"books"}
DELETE /books/:id(.:format) {:action=>"destroy", :controller=>"books"}
/:controller(/:action(/:id(.:format)))
def index
@books = Book.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @books }
end
end
<%= link_to 'New Book', new_book_path %>
<a href="/books/new">New Book</a>
<td><%= link_to 'Show', book %></td>
<td><a href="/books/1">Show</a></td>
<td><%= link_to 'Destroy', book, confirm: 'Are you sure?', method: :delete %></td>
<td><a href="/books/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a></td>
@book = Book.find(params[:id])
<%= render 'form' %>
@book = Book.new(params[:book])
def create
@book = Book.new(params[:book])
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render json: @book, status: :created, location: @book }
else
format.html { render action: "new" }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
<p id="notice"><%= notice %></p>
def update
@book = Book.find(params[:id])
respond_to do |format|
if @book.update_attributes(params[:book])
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
format.json { head :ok }
<%= form_for(@book) do |f| %>
| ------------- | 空(new) | 値あり(edit) |
| 送信先アドレス | /books | /books/1 |
| HTTPメソッド | post | put(疑似) |
def destroy
@book = Book.find(params[:id])
@book.destroy
respond_to do |format|
format.html { redirect_to books_url }
format.json { head :ok }
end
end