I would like to contribute to Rails. Specifically I'd like to add a
feature, and before I write any code I'd like to receive some feedback from
you guys.
When using Single Table Inheritance, all path/url helpers for the parent
resource will not work for the child resources. For example:
class User < ActiveRecord::Base
end
class Student < User
end
class Professor < User
end
u = User.create
> s = Student.create
> p = Professor.create
> link_to 'User', u # user_path(u)
> link_to 'Student', s # student_path(s)
> link_to 'Professor, p # professor_path(p)
> form_for u # user_path(u)
> form_for s # student_path(s)
> form_for p # professor_path(p)
Let's see what happens when I add a nested resource named posts to User
class User < ActiveRecord::Base
has_many :posts
end
> form_for [ u, Post.new ] # user_posts_path
>
form_for [ s, Post.new ] # student_posts_path
> form_for [ p, Post.new ] # professor_posts_path
It might look good and obvious, but now see the routes:
resources :users do
resources :posts
end
> resources :students do
resources :posts
end
> resources :professors do
resources :posts
end
And if I add other nested resources the repetition will grow larger and
larger. On StackOverflow I was answered to use a loop
[ :users, :students, :professors ].each do |name|
> resources name do
> resources :posts
> end
> end
While this might be the intended behaviour, I can't see a reason why the
nested post route needs to have 3 helpers when it is owned from the parent.
I was also suggested to use the *url *option in form_for to specify the
parent path. However this will not work when polymorphic associations are
used.
My solution? Instead of creating tons of unnecessary paths, extend the :*as
*option of resources to be an Array, and allow many path helpers to be
mapped to the same route. So that we can write:
resources :user, as: [ 'users', 'students', 'professors' ] do
> resources :posts
> end
This code should generate all the set of routes for the users resources.
And generate 3 sets of path helpers. users_path, students_path and
professors_path will all point to /users for example.
Do you think this will be useful to someone other than me? Do you believe
that DHH would allow such a thing :) ?
Thank you for your attention.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.