Skip to content

Commit

Permalink
03-1-2
Browse files Browse the repository at this point in the history
  • Loading branch information
xdite committed May 31, 2013
1 parent e967f83 commit 2f6170b
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 5 deletions.
36 changes: 35 additions & 1 deletion manuscript/chapter-03-1-0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,38 @@ gem 'devise'
<li> <%= link_to( "Sign Up" ,new_user_registration_path) %> </li>
<li> <%= link_to( "Login", new_user_session_path ) %> </li>
<li> <%= link_to("Logout",destroy_user_session_path, :method => :delete ) %> </li>
~~~~~~~~~
~~~~~~~~~


### devise 相關 method

1. 判斷現在使用者是否登入了,可以使用 current_user.blank?。
2. 要取現在這個登入的使用者資料,可以使用 current_user

### login_required

Boostrappers 在 `app/controller/application_controller.rb` 也先預幫開發者準備好一個 method:`login_required`。


~~~~~~~~~

def login_required
if current_user.blank?
respond_to do |format|
format.html {
authenticate_user!
}
format.js{
render :partial => "common/not_logined"
}
format.all {
head(:unauthorized)
}
end
end

end

~~~~~~~~~

如果開發者只是單純想限制哪一個 action 需要登入才能使用,只要掛上 before_action ,再指定即可。
23 changes: 23 additions & 0 deletions manuscript/chapter-03-1-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{::pagebreak :/}

## Ch 3.1.1 對需要加入登入的 Action 加入限制。

根據需求:使用者必須能夠 註冊 / 登入,登入後才可以開設 Group,與發表 Post。

所以我們要在 Groups controller 加入:

~~~~~~~~~
class GroupsController < ApplicationController

before_action :login_required, :only => [:new, :create, :edit,:update,:destroy]
~~~~~~~~~

在 Posts controller 加入:

~~~~~~~~~
class PostsController < ApplicationController

before_action :login_required, :only => [:new, :create, :edit,:update,:destroy]
~~~~~~~~~


178 changes: 178 additions & 0 deletions manuscript/chapter-03-1-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{::pagebreak :/}

## Ch 3.1.2 讓 Group 與 User 產生關聯:

新增一條 migration `rails g migration add_user_id_to_group`

~~~~~~~~~
invoke active_record
create db/migrate/20130531141923_add_user_id_to_group.rb
~~~~~~~~~

填入以下內容

~~~~~~~~~
class AddUserIdToGroup < ActiveRecord::Migration
def change
add_column :groups, :user_id, :integer
end
end
~~~~~~~~~

執行 `rake db:migrate`

~~~~~~~~~
== AddUserIdToGroup: migrating ===============================================
-- add_column(:groups, :user_id, :integer)
-> 0.0213s
== AddUserIdToGroup: migrated (0.0214s) ======================================
~~~~~~~~~


修改 `app/models/user.rb` 加入

~~~~~~~~~
has_many :groups
~~~~~~~~~

內容如下

~~~~~~~~~
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable

has_many :groups

extend OmniauthCallbacks

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable


end
~~~~~~~~~

修改 `app/models/group.rb` 加入

~~~~~~~~~
belongs_to :owner, :class_name => "User", :foreign_key => :user_id


def editable_by?(user)
user && user == owner
end

~~~~~~~~~


內容如下:

~~~~~~~~~
class Group < ActiveRecord::Base

belongs_to :owner, :class_name => "User", :foreign_key => :user_id
has_many :posts

validates :title, :presence => true


def editable_by?(user)
user && user == owner
end
end


~~~~~~~~~

接著我們要把 Groups 的幾個 action 內容替換掉:

### create

~~~~~~~~~

def create
@group = current_user.groups.build(group_params)
if @group.save
redirect_to groups_path
else
render :new
end
end
~~~~~~~~~

### edit

~~~~~~~~~
def edit
@group = current_user.groups.find(params[:id])
end
~~~~~~~~~


### update

~~~~~~~~~
def update
@group = current_user.groups.find(params[:id])

if @group.update(group_params)
redirect_to group_path(@group)
else
render :edit
end
end
~~~~~~~~~


### destroy

~~~~~~~~~
def destroy
@group = current_user.groups.find(params[:id])

@group.destroy

redirect_to groups_path
end
~~~~~~~~~

把 app/views/groups/index.html.erb 的內容換掉

~~~~~~~~~

<div class="span12">
<div class="group">
<%= link_to("New group", new_group_path , :class => "btn btn-mini btn-primary pull-right")%>
</div>
<table class="table">
<thead> <tr>
<td> # </td>
<td> Title </td>
<td> Descroption </td>
</tr>
</thead>
<tbody>
<% @groups.each do |group| %>
<tr>
<td> # </td>
<td> <%= link_to(group.title, group_path(group)) %> </td>
<td> <%= group.description %> </td>
<td>
<% if current_user && group.editable_by?(current_user) %>
<%= link_to("Edit", edit_group_path(group), :class => "btn btn-mini")%>
<%= link_to("Delete", group_path(group), :class => "btn btn-mini", :method => :delete, :confirm => "Are you sure?" ) %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

~~~~~~~~~

### 解說

TODO
8 changes: 4 additions & 4 deletions manuscript/chapter-03.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# 練習作業 3 - 為 Group 加入使用者機制
# 練習作業 3 - 為 Group 與 Post 加入使用者機制

在上一章我們完成了在 Group 裡面發表文章的功能。但是通常一個討論區的機制,必須是先加入會員才能進行相關動作。所以我們必須為討論區加入使用者機制:

使用者必須能夠 註冊 / 登入,登入後才可以發表 Post,不然只能瀏覽。只有自己的 Post 才能進行修改與刪除。
使用者必須能夠 註冊 / 登入,登入後才可以開設 Group,與發表 Post,不然只能瀏覽。只有自己的 Group,Post 才能進行修改與刪除。

## 練習主題
### 本章練習主題

* 安裝 gem
* 設定 devise
* 撰寫全域的 method `login_required`
* 利用 before_action 結合 login_required 加入登入判斷
* session 的使用:current_user

## 參考資料
### 本章參考資料

* [devise](https://github.com/plataformatec/devise/wiki)

Expand Down

0 comments on commit 2f6170b

Please sign in to comment.