Rails101 第五讲 Account group & Post 清单

Rails101 第五讲 Account group & Post group

目标

1.了解如何实做, Account group & Post group。

2.通过反复练习,能够熟记 Account group & Post group的核心代码。

1. 使用者可以在下拉选项单中看到自己参加的所有GROUP。

1)

app/views/common/_navbar.html.erb

+<li><%= link_to("My Groups", account_groups_path) %></li>
+<li class="divider"></li>
<li> <%= link_to("退出", destroy_user_session_path, method: :delete) %> </li>

config/routes

namespace :account do
   resources :groups
end 

2)

rails g controller account::groups

app/controllers/account/groups/groups_controller.rb

def index 
  @groups = current_user.participated_groups
end 
def edit 
  @group = Group.find(params[:id])
end 
def update
  @group = Group.find(params[:id])
  if @group.update(group_params)
    redirect_to account_groups_path
  else
    render :edit
  end 
end 
def destroy
  @group = Group.find(params[:id])
  @group.destroy
  flash[:alert] = "Group is deleted"
  redirect_to account_groups_path
end 
def group_params
  params.require(:group).permit(:title, :description)
end 

3)

A.

touch app/views/account/groups/index.html.erb

app/views/account/groups/index.html.erb

<div class="col-md-12">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Title</th>
        <th>Description</th>
        <th>Posts</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <% @groups.each do |group| %>
      <tr>
        <td><%= link_to(group.title, group_path(group))%></td>
        <td><%= group.description %></td>
        <td><%= group.posts.count %></td>
        <td>
            <%= link_to("Edit", edit_account_group_path(group), class: "btn btn-xs btn-default pull-right")%>
            <%= link_to("Delete", account_group_path(group), class: "btn btn-xs btn-default pull-right", method: :delete, data: {confirm: "Are you sure? "})%>
        </td>
      </tr>
      <% end %>
    </tbody>
  </table>
</div>

B.

touch app/views/account/groups/edit.html.erb

app/views/account/groups/edit.html.erb

<div class="col-md-4 col-md-offset-4">
  <h2>Edit a Group </h2>
  <hr>
  <%= simple_form_for [:account,@group] do |f| %>
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.submit "Submit", class: "btn btn-sm btn-primary" %>
  <% end %>
</div>

2. 使用者可以在下拉选项单中看到自己发表的所有POST。

1)

app/views/common/_navbar.html.erb

+<li><%= link_to("My Posts", account_posts_path) %></li>
+<li class="divider"></li>

config/routes

namespace :account do
   resources :groups
  +resources :posts
end 

2)

rails g controller account::posts

app/controllers/account/posts/posts_controller.rb

before_action :authenticate_user!
def index 
  @posts = current_user.posts
end 

3)

touch app/views/account/posts/index.html.erb

app/views/account/posts/index.html.erb

<div class="col-md-12">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Content</th>
        <th>Created Time</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <% @posts.each do |post| %>
      <tr>
        <td><%= post.content %></td>
        <td><%= post.created_at %></td>
        <td>
          <%= link_to("Edit", edit_group_post_path(post.group, post), class: "btn btn-xs btn-default pull-right")%>
           <%= link_to("Delete", group_post_path(post.group, post), class: "btn btn-xs btn-default pull-right", method: :delete, data: {confirm: "Are you sure? "})%>
        </td>
      </tr>
      <% end %>
    </tbody>
  </table>
</div>

备注:

如果报错则

rails c

Group.destroy_all

Post.destroy_all

exit

4)

conclusion

git add .

git commit -m "implement account group & post "

课后作业

1.复制粘贴教程中的代码。 (30MIN)

1)目的一:确保代码是正确的 & 了解代码产生的效果。

2)目的二: 用笔记本记录自己不清楚的代码。

2.与我ZOOM,一起讨论不清楚的代码。(15MIN)

3.写ORID,总结此次练习的收获,感悟,低峰,以及下一步行动(下一步行动中需包含此次课后作业)。

4.自己反复练习”第五讲 Account group & Post group“中的代码,直至能够在不看代码的情况下,自己默写出代码。

写在最后

我们之所以没有进步,或者进步缓慢,就是因为我们对自己要求太低了。 将 “第五讲 Account group & Post group”记住后,我相信,再去练习教程时,你会发现,自己居然突然懂了很多。加油吧!

Comments

comments powered by Disqus