Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished tags book

  • Loading branch information...
commit 983d1b91882914ebcaf03d1e244be5fcce6b8ad2 1 parent efb3d2d
@cantin authored
View
10 app/controllers/books_controller.rb
@@ -1,5 +1,5 @@
class BooksController < ApplicationController
- skip_before_filter :authenticate_user!, only: [ :index, :show, :search ]
+ skip_before_filter :authenticate_user!, only: [ :index, :show, :search, :tags ]
def index
@books = Book.includes(:user).page(params[:page]).per(10)
@mc_books = Book.most_commented_books 10
@@ -13,6 +13,14 @@ def search
@books = Book.search(q).result
end
+ def tags
+ @books = Book.tagged_with(params[:name]).page(params[:page]).per(10)
+ @mc_books = Book.most_commented_books 10
+ @tags = Book.tag_counts_on(:tags)
+
+ render 'homes/index'
+ end
+
# GET /books/1
# GET /books/1.json
def show
View
2  app/views/books/_tags.html.haml
@@ -1,5 +1,5 @@
.tag-list
%h3 标签
- tags.each do |tag|
- = link_to tag.name, '/'
+ = link_to tag.name, "/books/tags/#{tag.name}"
View
2  app/views/books/tags.html.haml
@@ -0,0 +1,2 @@
+.content
+ = render partial: 'books/list', locals: { books: @books, page: true }
View
1  config/routes.rb
@@ -3,6 +3,7 @@
match '/books/search' => 'books#search', as: :book_search, via: :post
resources :books
resources :homes
+ get "/books/tags/:name" => 'books#tags'
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
View
25 spec/requests/books/tag_books_spec.rb
@@ -0,0 +1,25 @@
+#encoding: utf-8
+require 'spec_helper'
+
+feature 'Tag books',driver: :chrome do
+ describe 'with valid tag' do
+ before(:all) do
+ 5.times do |i|
+ FactoryGirl.create :book, name: "张三的书#{i}", user_id: 1, tag_list: '技术'
+ end
+ FactoryGirl.create :book, name: "李四的书", user_id: 1
+ end
+
+ scenario 'will get the books' do
+ visit '/books'
+
+ click_link '技术'
+
+ 5.times do |i|
+ page.should have_content "张三的书#{i}"
+ end
+ page.should_not have_content '李四的书'
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.