Inserting a variable into routes - map.resources: posts ,: as => X - is it possible?
Ok, so I am working on a blog application. So far, it allows the user to register for their account, create posts, tags, comments, etc.
I just implemented the ability to use www.myapp.com/brandon to set @user to look up by username and therefore display user information correctly on each url. So when you visit www.myapp.com/brandon, you see all Brandon posts, tags and comments related to those posts, etc. Works great.
I am implementing this URL mapping through my routes.rb file by adding the following:
map.username_link '/:username', :controller => 'posts', :action => 'index'
And then just set the @user variable in PostController and the corresponding find_by_username views. Now this is the problem. Sometime on www.myapp.com/brandon, when you click on the post header, it sends to myapp.com/posts/id with no username in the url.
How can I specify rails to replace / posts / username.
Is it possible to insert the user_username variable into this code?
map.resources :posts, :as => [what goes here]
a source to share
You said there would be more to the page than just posts
? comments
and tags
also? It looks like we need some kind of resource aggregation ...
Another problem: what if a user chooses a name faq
and you want domain.com/faq
to go down the road? You may not know all the URLs you will need in the future. Using prefix paths /profiles
is a great way to create a small namespace to prevent this from happening. So that...
Why not ProfilesController
?
script/generate controller profiles index show
routes.rb
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
This will give you the following routes
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"}
profiles_controller.rb
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
posts_controller.rb (and others)
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
a source to share
You can create a link using:
= link_to "User Posts", subdomain_link_url(@user.username, @post)
In yours PostController
, then I would use before_filter
to search and set a variable @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
a source to share
I don't know much about routes and things, so I'm sorry if that doesn't make sense, but doesn't it work for you?
map.resources :posts, :path_prefix => '/:username' do |post|
post.resources :comments
end
I see here that this will generate the following
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"}
a source to share