forked from xdite/rails-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter-03-3.txt
193 lines (126 loc) · 3.41 KB
/
chapter-03-3.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{::pagebreak :/}
## Ch 3.3 讓 Post 與 User 產生關聯:
新增一條 migration: `rails g migration add_user_id_to_post`
~~~~~~~~~
invoke active_record
create db/migrate/20130531153435_add_user_id_to_post.rb
~~~~~~~~~
填入以下內容
~~~~~~~~~
class AddUserIdToPost < ActiveRecord::Migration
def change
add_column :posts, :user_id, :integer
end
end
~~~~~~~~~
執行 `rake db:migrate`
~~~~~~~~~
== AddUserIdToPost: migrating ================================================
-- add_column(:posts, :user_id, :integer)
-> 0.0176s
== AddUserIdToPost: migrated (0.0177s) =======================================
~~~~~~~~~
修改 `app/models/user.rb` 加入 `has_many :posts`
內容如下
~~~~~~~~~
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
has_many :groups
has_many :posts
extend OmniauthCallbacks
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
end
~~~~~~~~~
修改 `app/models/post.rb` 加入
~~~~~~~~~
belongs_to :author, :class_name => "User", :foreign_key => :user_id
def editable_by?(user)
user && user == author
end
~~~~~~~~~
內容如下:
~~~~~~~~~
class Post < ActiveRecord::Base
belongs_to :group
validates :content, :presence => true
belongs_to :author, :class_name => "User", :foreign_key => :user_id
def editable_by?(user)
user && user == author
end
end
~~~~~~~~~
{::pagebreak :/}
接著我們要把 Posts 的幾個 action 內容替換掉:
### create
~~~~~~~~
def create
@post = @group.posts.new(post_params)
@post.author = current_user
if @post.save
redirect_to group_path(@group)
else
render :new
end
end
~~~~~~~~
### edit
~~~~~~~~
def edit
@post = current_user.posts.find(params[:id])
end
~~~~~~~~
### update
~~~~~~~~
def update
@post = current_user.posts.find(params[:id])
if @post.update(post_params)
redirect_to group_path(@group)
else
render :edit
end
end
~~~~~~~~
{::pagebreak :/}
### destroy
~~~~~~~~
def destroy
@post = current_user.posts.find(params[:id])
@post.destroy
redirect_to group_path(@group)
end
~~~~~~~~
把 app/views/groups/show.html.erb 的內容換掉
~~~~~~~~
<div class="span12">
<div class="group pull-right">
<% if current_user && @group.editable_by?(current_user) %>
<%= link_to("Edit", edit_group_path(@group) , :class => "btn btn-mini ")%>
<% end %>
<%= link_to("New Post", new_group_post_path(@group) , :class => "btn btn-mini btn-primary" if current_user )%>
</div>
<h2> <%= @group.title %> </h2>
<p> <%= @group.description %> </p>
<table class="table">
<tbody>
<% @posts.each do |post| %>
<tr>
<td>
<span clas="author"> <strong> Author : <%= post.author.name %> </strong> </span>
<p>
<%= post.content %>
</p>
</td>
<% if current_user && post.editable_by?(current_user) %>
<td> <%= link_to("Edit", edit_group_post_path(post.group, post), :class => "btn btn-mini")%>
<%= link_to("Delete", group_post_path(post.group, post), :class => "btn btn-mini", :method => :delete, :confirm => "Are you sure?" ) %> </td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
~~~~~~~~