在views/cpanel/node
视图和model/node.rb
模型中分别添加f.slug
和field :slug
后,再在 topic.rb 模型中 cache node_slug,就可以通过修改 routes 来 cheat 了:
{"movies" => 1, "books" => 2, "music" => 3}.each do |slug, id|
match "#{slug}" => "topics#node", :as => "#{slug}".to_sym, :id => id
match "#{slug}/new" => "topics#new", :as => "new_#{slug}".to_sym, :node_id => id
end
以及 feed 的 routs:
{"movies" => 1, "books" => 2, "music" => 3}.each do |slug, id|
match "#{slug}/feed" => "topics#node_feed", :as => "feed_#{slug}".to_sym, :id => id
end
然后,修改一下render_node_name
方法
def render_node_name(name, slug)
link_to(name, "/#{slug}" )
end
在views/topics/_topic.html.erb
以及views/topics/_topic_info.html.erb
中调用render_node_name
的参数 node_id 改为 node_slug
最后,再修改一下render_topic_node_select_tag
方法:
def render_topic_node_select_tag(topic, node_id)
return if topic.blank?
# 我喜欢扁平式的node结构,所以取消了group_collection_select,而使用了collection_select
# grouped_collection_select :topic, :node_id, Section.all,
# :sorted_nodes, :name, :id, :name, :value => topic.node_id,
# :include_blank => true, :prompt => "选择节点"
collection_select :topic, :node_id, Node.all,
:id, :name, :value => topic.node_id,
:include_blank => true, :prompt => "选择主题类型",
:selected => node_id
end
这样,就支持类似
ruby-china.org/slug
ruby-china.org/slug/new
这样的访问方式啦。