view-partial/helper/scope

Partial
Step 1: 产生一个要调用的partial
eg: app/views/groups/_form.html.erb

touch app/views/groups/_form.html.erb

填入以下内容:

app/views/groups/_form.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%= form_for @group do |f| -%>
<% if @group.errors.any? %>
<ul>
<% @group.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
标题
<%= f.text_field :title %>
<br>
叙述
<br>
<%= f.text_area :description %>
<br>

<%= f.submit "Submit", :disable_with => 'Submiting...' %>
<% end %>

Step 2: 修改 app/views/groups/new.html.erb
修改 app/views/groups/new.html.erb,把原先重复的表单程式码“整段删掉”,改成 <%= render “form” %>

app/views/groups/new.html.erb
1
2
3
4
5
<div class="col-md-4 col-md-offset-4">
<h2>新增讨论版</h2>
<hr>
<%= render "form" %>
</div>

Step 3: 修改 app/views/groups/edit.html.erb

修改 app/views/groups/edit.html.erb,把原先重复的表单程式码“整段删掉”,改成 <%= render “form” %>

app/views/groups/edit.html.erb
1
2
3
4
5
<div class="col-md-4 col-md-offset-4">
<h2>编辑讨论版</h2>
<hr>
<%= render "form" %>
</div>

Step 4: git 存档

git add .
git commit -m “move form to partial”

ps:partial 还可以这样用
上面我们介绍了,form 可以放入 partial。这里我们要介绍 partial 还可以用在回圈上。
Step 1. 修改 app/views/groups/index.html.erb

把 @groups.each 内整段砍掉,换成 partial

app/views/groups/index.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="col-md-12">
<div class="group">
<%= link_to("New group", new_group_path, class: "btn btn-primary pull-right") %>
</div>
<table class="table table-hover">
<thead>
<tr>
<td>#</td>
<td>Title</td>
<td>Description</td>
<td>Creator </td>
</tr>
</thead>
<tbody>
<%= render :partial => "group_item", :collection => @groups, :as => :group %>
</tbody>
</table>
</div>

然后 touch app/views/groups/_group_item.html.erb

app/views/groups/_group_item.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
<tr>
<td>#</td>
<td><%= link_to(group.title, group_path(group)) %></td>
<td><%= render_group_description(group) %></td>
<td><%= group.user.email %></td>
<td>
<% if current_user && current_user == group.user %>
<%= link_to("Edit", edit_group_path(group), class: "btn btn-sm btn-default")%>
<%= link_to("Delete", group_path(group), class: "btn btn-sm btn-default",
method: :delete, data: { confirm: "Are you sure?" } )%>
<% end %>
</td>
</tr>

Step 2. git 存档
git add .
git commit -m “render partial with collection”


Helper
Step 1. 自制 render_group_description(group)
修改 app/helpers/groups_helper.rb
加入:

app/helpers/groups_helper.rb
1
2
3
4
5
module GroupsHelper
def render_group_description(group)
simple_format(group.description)
end
end

然后换掉 app/views/groups/index.html.erb 与 app/views/account/groups/index.html.erb 里关于 group.description 的部分:

app/views/groups/index.html.erb
1
2
3
<td><%= link_to(group.title, group_path(group)) %></td>
<td><%= render_group_description(group) %></td>
<td> <%= group.user.email %> </td>

Step 2 : git 储存

git add .
git commit -m “use self delimit helper”

ps:
自制的 Helper 是什么意思?
Helper 就是用 Ruby 写的“View 装饰的小方法”,你也可以自己定义
自制 Helper 都会放在 app/helpers 下

form-helper

1.checkbox 多选 与 radio button 单选

多选的例子:

<%= check_box_tag(:banana) %>
<%= label_tag(:banana, “我喜欢香蕉”) %>
<%= check_box_tag(:orange) %>
<%= label_tag(:orange, “我喜欢橘子”) %>

单选的例子:

<%= radio_button_tag(:sex, “male”) %>
<%= label_tag(:sex_male, “男”) %>
<%= radio_button_tag(:sex, “female”) %>
<%= label_tag(:sex_female, “女”) %>

2.下拉菜单:
1)自己给的选项
<%= select_tag ‘sex’, options_for_select([‘男’, ‘女’]) %>
2)在已有的信息中调取选项
<%= options_from_collection_for_select Book.all, :id, :title %>

3.填写信息框:
text_area比text_field框大
且text_field会自带默认值
text_field_tag要加默认值就要如下写:
<%= text_field_tag ‘article[title]’, ‘三体’ %>


Scope:在model中定义scope,controller中调用

app/models/post.rb
1
2
3
4
class Post < ApplicationRecord
# ... 略
scope :recent, -> { order("created_at DESC")}
end