Ask A Question

Notifications

You’re not receiving notifications from this thread.

Forum Series episodes ... no method error: undefined method

Miguel asked in Rails

I've been following along the Forum series of episodes, when I try to submit a forum post, I get an error. It seems that the controller has trouble assigning the user id to the current user. I thought my associations were wrong, or had a typo but I tried to litteraly copy-paste Chris's code from the Github repo and nothing. What the hell is going on??

user model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :forum_threads
  has_many :forum_posts
end

forum threads model

class ForumThread < ActiveRecord::Base
  belongs_to :user
  has_many :forum_posts
  has_many :users, through: :forum_posts

  accepts_nested_attributes_for :forum_posts

  validates :subject, presence: true
  validates_associated :forum_posts
end

I also tried scratching the through: :forum_posts and the whole has_many :users, through: :forum_posts... nope

forum posts model

class ForumPost < ActiveRecord::Base
    belongs_to :user 
    belongs_to :forum_thread    
end

The part of the ForumThreadsController that seems to raise the problem

        def create 
        @forum_thread = current_user.forum_threads.new forum_thread_params
        @forum_thread.forum_posts.first.user_id = current_user.id

        if @forum_thread.save 
            redirect_to @forum_thread 
        else 
            render action: :new 
        end
    end 
        ```
[Here's the repo](http://github.com/mickeytgl/gorails_forum)
Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 73,723+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.