creating a facebook pages like app in rails - ruby-on-rails

I have a rails app where users can sign up and create posts, now what i want to achieve is that i want to give users option to create pages and those pages will be able to create post, what i have done till now is i have generated another controller called pages i have added users to that pages by doing a migration.
Relationship between modals look like:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook, :twitter, :google_oauth2]
act_as_mentionee
validates :username, presence: true
validate :avatar_image_size
has_many :posts, dependent: :destroy
has_many :pages, dependent: :destroy
end
pages.rb
class Page < ActiveRecord::Base
validates :name, :description, :user_id, presence: true
belongs_to :user
has_many :posts, dependent: :destroy
delegate :username, to: :user
end
posts.rb
class Post < ActiveRecord::Base
act_as_mentioner
validates :title, :body, :user_id, presence: true
belongs_to :user
belongs_to :channel
end
Now i want to add posts to pages and i really don't know how to do that, i want to add posts in pages so pages can create post.
what will be the best way to go around this?
and if you know some kind of tutorial or gem for this please tell me.

First you need add a controller for User Model
to add a controller you can refer this rails guide
http://guides.rubyonrails.org/layouts_and_rendering.html
once controller is added you need to add your routes in routes.rb
resources :users
You need to add the corresponding views for each action in controller
In user model you can add
nested attributes for the :pages and :posts
see the below help document for nested attributes
https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
Hope this answer gives you some idea on how to proceed further

Related

Devise will not save to database due to belongs_to relationship

After adding the marked line to my Devise user model in a Rails app I can no longer create new users in either the web form or using User.create() on the console. Commenting out this line restores full functionality. Obviously it is very closely related to the line above, it is so that I can have users assigned to a dealership. Any suggestions how to fix or do it differently as I need this functionality?
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable
has_many :tanks, dependent: :destroy
has_many :properties, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :stations
has_many :clients, :class_name => 'User', :foreign_key => :manager_id
belongs_to :dealer, :class_name => 'User' #<--- this one
end
belongs_to :dealer, :class_name => "User",foreign_key: "dealer_id", optional: true
Note: - make sure you should have dealer_id in User model also to make it working #user.dealer if #user.dealer.present?
Reason
In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default.

User has_many association not working (error:Could not find the association :user_categories in model Category)

I have three models and here they are when I try to create a has_many. I basically want my users (using devise) to have many categories. And categories to have many users.
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
has_many :user_categories
has_many :categories, through: :user_categories
acts_as_messageable
def mailboxer_email(object)
email
end
end
userCategory.rb
class UserCategory < ActiveRecord::Base
belongs_to :user
belongs_to :category
accepts_nested_attributes_for :categories
end
Category.rb
class Category < ActiveRecord::Base
has_many :user_categories
has_many :user, through: :user_categories
validates :name, presence: true, length: {minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
when I run category.users << user I get this error:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :user_categories in model Category
I can't say for sure what the problem could be, but a few things I could point out:
UserCategory's accepts_nested_attributes_for, does that mean the you want to be able to dynamically create categories?
Category has_many :users, through: :user_categories, not user
You need to follow the Rails file naming conventions, user.rb, user_category.rb and category.rb
These may not be the problem/solution, but I believe they're in the way of resolving the problem.

Dynamic root for authenticated polymorphic attribute

I have three models as follows:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role, polymorphic: true
validates_presence_of :first_name, :last_name, :email, :password,
:password_confirmation
end
student.rb
class Student < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy
accepts_nested_attributes_for :user
end
teacher.rb
class Teacher < ActiveRecord::Base
has_one :user, as: :role, dependent: :destroy
accepts_nested_attributes_for :user
end
I have user registration and login working as expected. However, I cannot figure out how to direct users to the appropriate homepage when they're logged in.
I handle routing for authenticated users as follows:
authenticated :user do
root to: "students#home", as: :user_root
end
Ideally if current user's role attribute is a student, then it sets students#home as :user_root. If it's a teacher, then it sets teachers#home as :user_root. Is there any way to handle this purely in routes?
They way you've structured it seems confusing to me. How much do teachers and students really have in common? Won't students and teachers need different associations, fields, and everything?
Why not have completely separate Student and Teacher models with different devise scopes? See devise wiki configuring multiple models: https://github.com/plataformatec/devise#configuring-multiple-models
For shared functionality you can have both inherit from an abstract model or use concerns / modules for shared functionality.
module DeviseConcern
extend ActiveSupport::Concern
included do
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name, :email, :password,
:password_confirmation
end
end
While playing around with AJcodez's solution, some things like user authentication were less than pleasant to implement. I ended up opting for my original implementation.
One lesser known things about routes is that they can have lambas in them. Our routes ended up looking like the following:
authenticated :user, lambda { |u| u.role_type == "Student" } do
root to: "students#home", as: :student_root
end
authenticated :user, lambda { |u| u.role_type == "Teacher" } do
root to: "teachers#home", as: :teacher_root
end

How do I go about allowing a user to make a profile in Rails?

I want to make a profile for a user I generated with Devise and did this (in Rails 4):
rails g scaffold Profile first_name:string last_name:string school:string user:references
and then this:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profiles
end
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :first_name, :last_name, :school #where does this go in rails 4?
end
I want to know if this is the right way of doing this type of thing, or what should I do instead?
I also considered just adding all these fields directly to the User model through a migration...
Please any links and suggestions to do this correctly would really help!
I think you should go with a Users Profile page.
$ rails generate controller Users show
and in your routes file
devise_for :users
match 'users/:id' => 'users#show', as: :user
Users controller
#user = User.find(params[:id])

Rails Devise polymorphic

I am using devise and I want create a polymorphic relationship, I added the columns to table users 'usersable_type' and 'usersable_id'
This is my code
Model >> User
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#usarsable
belongs_to :usersable, :polymorphic => true, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :usersable
end
Model >> Medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user, as: :usersable, dependent: :destroy
has_and_belongs_to_many :patients
end
Model >> Patient
class Patient < ActiveRecord::Base
belongs_to :socialsecurity
attr_accessible :birthday, :blood_type
has_one :user, as: :usersable, dependent: :destroy
has_many :contacts
has_and_belongs_to_many :medics
end
Override Devise Controller
class RegistrationsController < Devise::RegistrationsController
def new
super
#user.build_usersable # I had problem in this line
end
def create
end
def update
super
end
end
Those are all the models I have for the moment, but I still have the same problem, I dont know how create and save the polymorphic object.
The error still is the same
ERROR: undefined method `build_usersable' for "<#User:"
Is anyone can help me I would be grateful
Regards and thanks in advance
Juli.
Based on the conversation in the comments this is the kind of scheme i think you need.
I am going to use the concept of Aspect, that is a user can have many aspects, the aspect might be a medic, a content detail etc; an Aspect can have only one user.
First you need a UserAspect model which has a user_id and a aspect_type and aspect_id
class UserAspect < ActiveRecord::Base
belongs_to :user
belongs_to :aspect, :polymorphic => true
end
Now your user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#aspects
has_many :user_aspects
has_many :aspects, :through => :user_aspects
end
And now your medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user_aspect, as: :aspect, dependent: :destroy
has_one :user, :through => :user_aspect
end
Now you can do things like
user.aspects
medic.user
etc

Resources