0

I am new at rails and I am trying to have a subpost inherited from a post. A post belongs_to user and has_many subposts. I am unsure of how to create the forms and controller that will allow the post become of type subpost.

I've tried looking at tutorials but I can't grasp my head around this problem.

Something I've looked at is this. : https://devblast.com/b/single-table-inheritance-with-rails-4-part-1/

Assume that the models are correct.

Here is how the routes look like

 Rails.application.routes.draw do
  resources :users do
    resources :post do
      resources :subpost, controller: :post, type: 'Subpost'
    end
  end

Here is the table

  create_table "posts", force: :cascade do |t|
    t.text "text"
    t.integer "user_id"
    t.integer "post_id"
    t.string "type"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["post_id"], name: "index_posts_on_post_id"
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

Form that I've been using:

<%= form_with (model: [ @user, @user.posts.build ], local: true) do |form| %>

I want the subpost to record user_id post_id and type properly on the table.

XXX
  • 47
  • 9
  • 1
    I would recommend you read the [notes on deep nesting](https://guides.rubyonrails.org/routing.html#limits-to-nesting) from the guides. You should really be using the shallow option here to limit nesting to one level. – max Jul 15 '19 at 08:34
  • @max , then is this how I should be doing my routing `resources :users do resources :posts end` `resources :posts do resources :subposts end` `resources :users do resources :subpost end` ? – XXX Jul 15 '19 at 09:46

0 Answers0