Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:02:43+00:00 2026-06-11T11:02:43+00:00

I’ve currently got: class Tutorial has_many :comments end class VideoTutorial < Tutorial end class

  • 0

I’ve currently got:

class Tutorial
  has_many :comments
end

class VideoTutorial < Tutorial
end

class Comments
  belongs_to :tutorial
end

routes.rb reads like:

resources :tutorials do
  resources :comments
end

I’d like to be able to refer to specific types of Tutorial (starting with VideoTutorial) like this:

/tutorials/1234
/tutorials/1234/comments/new
/tutorials/1234/comments/6374

Which means dealing with tutorials as Tutorial as much as possible, rather than VideoTutorial (or other subclasses as they come along).

I would like to have all calls going to a single controller and using straightforward routing as above.

PROBLEM: what is happening now

My forms seem to be matching routes polymorphically, matching the type of a specific Tutorial instance, e.g.

# @tutorial is a VideoTutorial
= form_for @tutorial do |f|  # undefined method 'video_tutorial_path'
...

Which is pretty cool but not what I’m looking for in this situation 🙂

I am currently making things work by generating those routes:

resources :tutorials do
  resources :comments
end

resources :video_tutorials, :controller => "tutorials" do
  resources :comments
end

I’m directing to the Tutorials controller as I’d like to avoid lots of controllers multiplying, when new Tutorial subclasses arise.

But this gets messy:

  • You get a lot of extra routes
  • You get more routes when you add a new subclass of Tutorial
  • You end up referencing params like :video_tutorial_id rather than just a generic :id

I’d like to treat all types of Tutorial as Tutorial in the above scenario.

What’s a simpler, less messy approach?

UPDATE: as per @jdl’s suggestion

Link to Tutorial show page:

  # original approach:
  = link_to 'Show', @tutorial

  # now:
  = link_to 'Show', tutorial_path(@tutorial)

form_for helper:

  # original approach:
  = form_for @tutorial do |f|

  # now:
  = form_for @tutorial, :as => :tutorial, :url => tutorial_path do |f|

form_for nested resources:

  # original approach:
  = form_for [@tutorial, @new_comment] do |f|

  # now:
  = form_for [@tutorial, @new_comment], :as => :tutorial, :url => tutorial_comments_path(@tutorial, @new_comment) do |f|

This works as intended now.

A bit more wordy 🙂 Any further thoughts as to making it more elegant?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:02:45+00:00Added an answer on June 11, 2026 at 11:02 am

    Take a look at the :as parameter of form_for.

    http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.