mirror of
https://github.com/wahyd4/Libr.git
synced 2026-08-09 04:46:07 +10:00
add books to library
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
$(document).ready(function(){
|
||||
|
||||
|
||||
var searchBook = function(){
|
||||
var query = $('.key-word').val();
|
||||
if(query.replace(/\s+/, "") ===""){
|
||||
return false;
|
||||
}
|
||||
$.ajax({
|
||||
url:'https://api.douban.com/v2/book/search?alt=xd&q='+ query,
|
||||
dataType:"jsonp",
|
||||
success:function(json){
|
||||
console.log(json);
|
||||
showBooks(json);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
var showBooks = function(json){
|
||||
$('.books').empty().append('<p>共<b>'+json.count+'</b>条结果</p>');
|
||||
var books = json.books;
|
||||
books = $.isArray(books) ? books : books.toArray()
|
||||
for(var i=0;i<books.length;i++){
|
||||
var book = $('<div class="row-fluid"></div>');
|
||||
var coverImage = $('<div class="span2"></div>');
|
||||
var info = $('<div class="span5"></div>');
|
||||
//info detail
|
||||
title = $('<h4>'+books[i].title+'</h4>');
|
||||
info.append(title);
|
||||
if(books[i].author && books[i].author.length > 0){
|
||||
var author = $('<p>作者:'+books[i].author[0]+'</p>');
|
||||
info.append(author);
|
||||
}
|
||||
if(books[i].isbn13){
|
||||
var isbn = $('<p>ISBN:'+books[i].isbn13+'</p>');
|
||||
info.append(isbn);
|
||||
}
|
||||
if(books[i].publisher){
|
||||
var publisher = $('<p>出版社:'+books[i].publisher+'</p>');
|
||||
info.append(publisher);
|
||||
}
|
||||
coverImage.append('<img class="cover" src="'+books[i].image+'" />');
|
||||
var tempForm = '<div class="span3"><form method="post" action="/books">'
|
||||
+'<input hidden="true" name="image" value="'+books[i].image+'"/>'
|
||||
+'<input hidden="true" name="title" value="'+books[i].title+'"/>'
|
||||
+'<input hidden="true" name="isbn" value="'+books[i].isbn13+'"/>'
|
||||
+'<button class="btn btn-primary add-to-lib"><i class="icon-plus"></i>添加至书库</button>'
|
||||
+'</form></div>';
|
||||
var addToLib = $(tempForm);
|
||||
book.append(coverImage).append(info).append(addToLib);
|
||||
$('.books').append(book).append('<hr>');
|
||||
|
||||
}
|
||||
$('.add-to-lib').bind('click',addBookToLib);
|
||||
}
|
||||
|
||||
$('.search-book').bind('click',searchBook);
|
||||
|
||||
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
$(document).ready(function(){
|
||||
|
||||
|
||||
loadBook = function(){
|
||||
var loadBook = function(){
|
||||
var isbn = $('input.isbn').val();
|
||||
if(isbn === null){
|
||||
return;
|
||||
@@ -15,7 +15,7 @@ $(document).ready(function(){
|
||||
}
|
||||
});
|
||||
}
|
||||
displayBookInfo = function(json){
|
||||
var displayBookInfo = function(json){
|
||||
$('img.cover').attr('src',json.images.large);
|
||||
var ul = $('div.span4 ul');
|
||||
ul.append('<li><b>副标题:</b>'+json.subtitle+'</li>');
|
||||
|
||||
@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
|
||||
@current_user = User.find_by_name session[:name]
|
||||
end
|
||||
|
||||
puts "!!~~~~~~~" +@current_user.to_s
|
||||
end
|
||||
|
||||
def sign_in(name,avatar)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class BookController < ApplicationController
|
||||
before_filter :current_user
|
||||
skip_before_filter :verify_authenticity_token ,:only => [:add_to_lib]
|
||||
|
||||
def view
|
||||
@book = Book.find_by_id params[:id]
|
||||
@@ -29,6 +30,21 @@ class BookController < ApplicationController
|
||||
@current_user.borrow book
|
||||
@msg = 'Borrowed success.'
|
||||
end
|
||||
redirect_to :back, notice: 'Borrowed book success.'
|
||||
redirect_to :back, notice: @msg
|
||||
end
|
||||
|
||||
def add
|
||||
render :add
|
||||
end
|
||||
|
||||
def add_to_lib
|
||||
unless @current_user
|
||||
redirect_to '/login',alert: "You need login."
|
||||
return
|
||||
end
|
||||
|
||||
@current_user.books.create name:params[:title], image:params[:image],isbn: params[:isbn]
|
||||
@msg = 'This book has been succeed to add to library.'
|
||||
redirect_to :back, notice: @msg
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
- content_for :javascript_includes do
|
||||
= javascript_include_tag 'add-book'
|
||||
|
||||
- if flash[:notice]
|
||||
div.alert.alert-success = flash[:notice]
|
||||
div.row-fluid
|
||||
div style="text-align:center"
|
||||
div.form-search style="margin:0 auto; display: inline-block;"
|
||||
div.input-append
|
||||
input.search-query.key-word type="text" style="width:22em" placeholder="请输入书名或者ISBN进行搜索"
|
||||
a.btn.search-book type="submit" 搜索
|
||||
div.books style="margin-bottom:1em;"
|
||||
@@ -19,6 +19,8 @@ html
|
||||
ul.nav
|
||||
li
|
||||
a href="" 书架
|
||||
li
|
||||
a href="/books/add" 添加图书
|
||||
ul.nav.pull-right
|
||||
-if @current_user != nil
|
||||
li
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
Libr::Application.routes.draw do
|
||||
|
||||
root to:'home#index'
|
||||
match 'books/add' => 'book#add'
|
||||
match 'books/:id' => 'book#view'
|
||||
post 'books' => 'book#add_to_lib'
|
||||
post 'books/:id/borrow' => 'book#borrow'
|
||||
get '/search/book/:arg' =>'book#search'
|
||||
get '/login' => 'user#login'
|
||||
|
||||
@@ -26,7 +26,6 @@ module Douban_Auth
|
||||
|
||||
response = http.post(path, data, headers)
|
||||
response = JSON.parse response.body
|
||||
puts "!!!!!!!!!!!!!!!!!!" + response.to_s
|
||||
response['access_token']
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user