Permalink
Browse files

Merge branch 'feature/show_user' into develop

  • Loading branch information...
2 parents 1c52673 + e52f35a commit 4c9c931fce61905b7f461b95f70540fcd6f4e6e6 @cantin committed Apr 16, 2012
@@ -1,3 +1,17 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
+
+jQuery ->
+ jQuery('.following-user').click ->
+ jQuery.ajax
+ type: 'post'
+ url: '/users/follow'
+ data: { id: jQuery(@).attr('value') }
+ error: (data) ->
+ alert('关注失败:' + data.responseText)
+ success: (data) ->
+ alert('关注成功')
+
+
+
@@ -1,5 +1,5 @@
class UsersController < ApplicationController
- skip_before_filter :authenticate_user!, only: :show
+ skip_before_filter :authenticate_user!, only: [:show, :index]
def index
@users = User.all
@@ -8,13 +8,24 @@ def index
def show
@user = User.find(params[:id])
- @follow_books = @user.following_by_type "Book"
+ @follow_user = @user.following_by_type 'User'
@upload_books = @user.books
+ @comment = @user.comments
end
def books
@books = current_user.books.page(params[:page]).per(10)
render 'homes/index'
end
+
+ def follow
+ user = User.find params[:id]
+
+ current_user.follow(user)
+
+ respond_to do |format|
+ format.json { render json: {}, status: :ok }
+ end
+ end
end
View
@@ -11,6 +11,8 @@ class Book < ActiveRecord::Base
acts_as_followable
acts_as_taggable
+ default_scope order "created_at desc"
+
class << self
def most_commented_books amount
Book.joins(:comments).group('books.id').order('count(comments.commentable_id) desc').limit(amount)
@@ -1,5 +1,5 @@
%li.dropdown
- = link_to current_user.name, '#', class: 'dropdown-toggle', 'data-toggle' => 'dropdown'
+ = link_to current_user.name, user_path(current_user), class: 'dropdown-toggle', 'data-toggle' => 'dropdown'
%ul.dropdown-menu
%li= link_to '我的书', '/user/books'
%li= link_to '用户信息设置', edit_user_registration_path
@@ -1,24 +1,18 @@
-.user-show
- .p "#{@user.name}关注的书"
- .ul
- = @follow_books.each do |b|
- .li= image_tag b.cover.url
- .li= b.name
- .li= b.author
- .li= b.author_abstract
- .li= b.ISBN
- .li= b.content_abstract
- .p "#{@user.name}上传的书"
- .ul
- = @upload_books.each do |b|
- .li= image_tag b.cover.url
- .li= b.name
- .li= b.author
- .li= b.author_abstract
- .li= b.ISBN
- .li= b.content_abstract
- .p "#{@user.name}评论的书"
- .ul
- = @commet.each do |c|
- .li "#{@user.name}对#{c.book.name}的评论"
- .li= c.commet
+.content
+ .user-show
+ %h3 #{@user.name}
+ = link_to '关注他', "javascript:void(0)", class: 'following-user', value: "#{@user.id}"
+ %h3 #{@user.name}关注的人
+ %ul
+ - @follow_user.each do |u|
+ %li
+ = image_tag u.avatar.small.url
+ = link_to u.name, user_path(u)
+ %h3 #{@user.name}上传的书
+ = render partial: 'books/list', locals: { books: @upload_books, page: false }
+ %h3 #{@user.name}评论的书
+ %ul
+ - @comment.each do |c|
+ %li
+ %p= "#{@user.name}对#{c.commentable.name}的评论"
+ %p= "评论:#{c.comment}"
View
@@ -1,6 +1,7 @@
Shu::Application.routes.draw do
get '/books/search' => 'books#search', as: :book_search
+ post '/users/follow' => 'users#follow', as: :user_follow_path
resources :books
resources :homes
get "/books/tags/:name" => 'books#tags'
View
@@ -2,13 +2,6 @@
tags = %w(计算机 文学 历史 哲学 心理学 随笔 科普)
-3.times.each do |i|
- Book.create(name: Faker::Product.product_name, content_abstract: Faker::Lorem.sentences(5),
- author_abstract: Faker::Lorem.sentences,
- author: Faker::Name.name, cover: File.open("#{Rails.root}/app/assets/images/rails.png"),
- user: User.first
- )
-end
100.times.each do |i|
b = Book.create(name: Faker::Product.product_name, author: Faker::Name.name, ISBN: rand(111111111111), user_id: rand(99)+1 )
@@ -21,3 +14,10 @@
b.save
end
+3.times.each do |i|
+ Book.create(name: Faker::Product.product_name, content_abstract: Faker::Lorem.sentences(5),
+ author_abstract: Faker::Lorem.sentences,
+ author: Faker::Name.name, cover: File.open("#{Rails.root}/app/assets/images/rails.png"),
+ user: User.first
+ )
+end
View
@@ -9,8 +9,12 @@
ActiveRecord::Base.transaction do
u = User.new(name: "shu", email: 'admin@shu.com', password: '123456', password_confirmation: '123456')
u.confirm!
- Book.create!(name: "ruby on rails", author: "someone", cover: File.open("#{Rails.root}/app/assets/images/rails.png"))
load File.join(File.dirname(__FILE__), "user_seeds.rb")
load File.join(File.dirname(__FILE__), "book_seeds.rb")
+
+ Book.create!(name: "ruby on rails", author: "someone",
+ cover: File.open("#{Rails.root}/app/assets/images/rails.png"),
+ user: u
+ )
end

0 comments on commit 4c9c931

Please sign in to comment.