ruby on rails - Nested routes that do not imply resource association -
is considered bad practice (or un-restful) create nested routes resources otherwise have no association? example have:
resources :foos resources :bars end
but have no business logic elsewhere in database or application associates :foos
:bars
.
the reason want this: many of routes created resources nested under :groups
resource. can grab group_id
param , show layout matches group user "in". i'm comfortable when resource belongs_to
group:
/groups/1/comments/1
but when other comment not belong_to
group (group1) , want @ through layout "branded" group1, impulse route this:
/groups/1/comments/2
is ok do, maybe i'm overthinking this?
i maintain app similar requirements. along lines of:
class user has_and_belongs_to_many :groups belongs_to :active_group, class_name 'group' def active_group return super unless super.nil? group = groups.first update_columns(active_group_id: group.id) group end end
the user
class validates assigned 1 or more groups , active_group
method overriden provide default if first login. approach need provide action set active group user can switch groups (presumably requirement).
this assumes have kind of authentication in place know current user. if restricting access groups not concern, can forego habtm relationship , substitute groups.first
group.first
.
if don't have/want/need authentication, drop active_group_id
in session cookie. consider bad practice nest unrelated resources.
Comments
Post a Comment