好的,所以我正在开发各种博客应用程序。到目前为止,它允许用户注册他们自己的帐户,创建帖子,标签,评论等。
我刚刚实现了使用www.myapp.com/brandon将@user设置为按用户名查找的功能,因此可以在每个网址上正确显示用户信息。因此,当您访问www.myapp.com/brandon时,您会看到所有Brandon的帖子,标签和与这些帖子相关的评论等。效果很好。
我正在通过routes.rb文件实现此URL映射,方法是添加以下内容:
map.username_link '/:username', :controller => 'posts', :action => 'index'
然后只需在PostController中设置@user变量,并将相应的视图设置为find_by_username。现在的问题是这个。访问www.myapp.com/brandon,当您点击帖子标题时,会发送到myapp.com/posts/id,而不会在网址中显示用户名。
如何告诉rails用/ username替换/ posts。
甚至可以将user_username变量插入此代码中吗?
map.resources :posts, :as => [what goes here]
答案 0 :(得分:4)
你说页面上的内容不仅仅是posts
吗? comments
和tags
也是{?}}听起来我们需要一些资源聚合......
另一个问题:如果用户选择名称faq
并且您希望domain.com/faq
在路上,会怎样?您不可能知道将来想要的所有URL。使用/profiles
前缀路径是构建一个小“命名空间”以防止这种情况发生的好方法。所以......
为什么不是ProfilesController
?
script/generate controller profiles index show
ActionController::Routing::Routes.draw do |map|
map.resources :profiles, :only => [:index, :show] do |profile|
profile.resources :posts, :only => [:index, :show]
profile.resources :comments, :only => [:index, :show]
profile.resources :tags, :only => [:index, :show]
end
# ...
end
这将为您提供以下路线
profiles GET /profiles(.:format) {:controller=>"profiles", :action=>"index"}
profile GET /profiles/:id(.:format) {:controller=>"profiles", :action=>"show"}
profile_posts GET /profiles/:profile_id/posts(.:format) {:controller=>"posts", :action=>"index"}
profile_post GET /profiles/:profile_id/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
profile_comments GET /profiles/:profile_id/comments(.:format) {:controller=>"comments", :action=>"index"}
profile_comment GET /profiles/:profile_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
profile_tags GET /profiles/:profile_id/tags(.:format) {:controller=>"tags", :action=>"index"}
profile_tag GET /profiles/:profile_id/tags/:id(.:format) {:controller=>"tags", :action=>"show"}
class ProfilesController < ApplicationController
# show all profiles; profile browser
# /profiles
def index
end
# show one profile
# /profiles/:id
def show
@user = User.find_by_username(params[:id])
end
end
class PostsController < ApplicationController
before_filter :find_profile, :only => [:index, :show]
# list all posts for this profile
# /profiles/:profile_id/posts
def index
end
# show one post for this profile
# /profiles/:profile_id/posts/:id
def show
end
protected
def find_profile
@user = User.find_by_username(params[:profile_id])
end
end
答案 1 :(得分:0)
您应该可以使用以下方式创建链接:
= link_to "User Posts", subdomain_link_url(@user.username, @post)
在PostController
中,我会使用before_filter
查找并设置@user
变量:
class PostController < ApplicationController
before_filter :find_user
def other_method
# Some code here
end
protected
def find_user
@user = User.find_by_username(params[:username])
end
end
答案 2 :(得分:0)
我对路线和东西知之甚少,所以请原谅我,如果这没有意义,但它不适合你吗?
map.resources :posts, :path_prefix => '/:username' do |post|
post.resources :comments
end
我在这里可以看到这会生成以下内容
posts GET /:username/posts(.:format) {:controller=>"posts", :action=>"index"}
POST /:username/posts(.:format) {:controller=>"posts", :action=>"create"}
new_post GET /:username/posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_post GET /:username/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
post GET /:username/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
PUT /:username/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
DELETE /:username/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
post_comments GET /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
POST /:username/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
new_post_comment GET /:username/posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
edit_post_comment GET /:username/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
post_comment GET /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
PUT /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
DELETE /:username/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}