Search code examples
ruby-on-railsdeviseprivate-messaging

Devise and simple-private-messages


I am using Devise in my Ruby on Rails 3 application. I am trying to implement private messaging in my application and I came across this gem:

https://github.com/jongilbraith/simple-private-messages

I (accidentally) ran the following command.

rails generate simple_private_messages:model User Message

It created the Message model. But it changed some properties of my existing User model that I had generated using Devise using the following command:

    rails generate devise User

Now, when I start my Ruby on Rails application I get this warning:

[WARNING] You provided devise_for :users but there is no model User defined in your application

And my Devise links have stopped working:

ActionView::Template::Error (undefined local variable or method `edit_user_registration_path' for #<#:0x1064c9490>):

Can someone please suggest how can I integrate the both or revert my changes if it is not possible to use them simultaneously?


Solution

  • I've followed these steps to install the gem:

    rails generate devise:install
    
    rails generate devise User
    
    rails generate simple_private_messages:model User Message
    

    Add this line (has_private_messages) to User Model:

    class User < ActiveRecord::Base
        # Include default devise modules. Others available are:
        # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
        devise :database_authenticatable, :registerable,
            :recoverable, :rememberable, :trackable, :validatable
    
        # Setup accessible (or protected) attributes for your model
        attr_accessible :email, :password, :password_confirmation, :remember_me
    
        has_private_messages
    

    end

    Edit the routes.rb file, the order is important here, devise_for should be defined before the messages routes.

    devise_for :users
    
    resources :users do
        resources :messages do
            collection do
                post :delete_selected
            end
        end
    end
    

    If you want the scaffold:

    rails generate simple_private_messages:scaffold User Message
    

    And remember to uncomment this (attr_accessor :to):

    class Message < ActiveRecord::Base
    
      is_private_message
    
      # The :to accessor is used by the scaffolding,
      # uncomment it if using it or you can remove it if not
      attr_accessor :to
    end