Skip to main content

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

Rails • Asked by Miguel

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)

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.