Can we use refinerycms for user authentication purpose - ruby-on-rails-3

Am developing one website for which I have to do user login and registration form for the authentication purpose. My question is can we do this using only refinerycms and if it is possible please tell me how to do this. If it is not possible then please tell me which is the best approach. Am trying to implement user authentication using refinerycms. Am new to refinerycms.

Refinery uses devise so you can use that as well. You can create a model that might look something like this:
module Refinery
module Partners
class Partner < Refinery::Core::BaseModel
self.table_name = 'refinery_partners'
acts_as_indexed :fields => [:name]
validates :email, :presence => true, :uniqueness => true
#devise methods
devise :database_authenticatable, :recoverable, :rememberable, :trackable,:validatable, :authentication_keys => [:email]
end
end
end
and not to forget the migration:
class CreatePartnersPartners < ActiveRecord::Migration
def up
create_table :refinery_partners do |t|
t.string :email
t.string :name
## Database authenticatable
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
t.timestamps
end
end
def down
if defined?(::Refinery::UserPlugin)
::Refinery::UserPlugin.destroy_all({:name => "refinerycms-partners"})
end
if defined?(::Refinery::Page)
::Refinery::Page.delete_all({:link_url => "/partners/partners"})
end
drop_table :refinery_partners
end
end
This should create the basic model that you can use. To finish up, change the routes.rb in your extension to add devise routes:
devise_for :partners, :class_name => "Refinery::Partners::Partner",
:controllers => {:sessions => 'refinery/partners/sessions', :passwords => 'refinery/partners/passwords'}
and override refinery/partners/passwords_controller.rb
module Refinery
module Partners
class PasswordsController < Devise::PasswordsController
end
end
end
and refinery/partners/sessions_controller.rb
module Refinery
module Partners
class SessionsController < Devise::SessionsController
end
end
end
now you should have everything you need

Related

Why does my users table have a no column error?

I am getting the following error after running my tests in the console:
ActiveRecord::StatementInvalid: SQLite3::SQLException: table users has no column named password: INSERT INTO "users"
user_test.rb:
class UserTest < ActiveSupport::TestCase
test "a user should enter a first name" do
user = User.new
assert !user.save
assert !user.errors[:first_name].empty?
end
test "a user should enter a last name" do
user = User.new
assert !user.save
assert !user.errors[:last_name].empty?
end
test "a user should enter a profile name" do
user = User.new
assert !user.save
assert !user.errors[:profile_name].empty?
end
test "a user should have a unique profile name" do
user = User.new
user.profile_name = users(:adam).profile_name
assert !user.save
assert !user.errors[:profile_name].empty?
end
end
users.rb:
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 :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
uniqueness: true
has_many :statuses
def full_name
first_name + " " + last_name
end
end
users.yml:
dan:
first_name: "Dan"
last_name: "Can"
email: "dan#email.com"
profile_name: "dan"
password: "123456"
password_confirmation: "123456"
database.yml:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
What I believe to be my user migrate file:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :profile_name
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
## Token authenticatable
# t.string :authentication_token
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
end
end
I would like to know what is causing the error, but more importantly why.
When I removed the password and password_confirmation columns from the users fixture it passed the test with no errors. I'm told by a friend that is was likely due to an upgrade in devise.
It seems to me that the problem you have is, just as the error says, there is no password column in your User table. You have to add it to your migration:
t.string :password
Note, however, that I have never used Devise so I may be wrong about this.
I think there are a couple of possibilities here but I will focus on the immediate problem. If your Rake tasks are returning No command found, then it may be because Rake isn't installed on your computer. I would start there.
To install Rake, type in your terminal:
gem install rake
The reason why your code doesn't work is because your users table doesn't have a column named password. With rake db:migrate and rake db:test:prepare you are ensuring that any migrations you created are applied to your database.
Let me know the results.

undefined method `zero?' for nil:NilClass on Devise

I'm installing Devise and I'm getting a undefined method `zero?' for nil:NilClass when trying to register a new user. Routing seams ok, as other pages such as sign in, sign up, lost password is showing up.
The error message:
NoMethodError in Devise::RegistrationsController#create
undefined method `zero?' for nil:NilClass
My Class User is as follows:
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 #, :encryptable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :password_salt
# attr_accessible :title, :body
end
The migration is running OK as:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
t.integer :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at
## Token authenticatable
t.string :authentication_token
t.string :password_salt
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
add_index :users, :confirmation_token, :unique => true
add_index :users, :unlock_token, :unique => true
add_index :users, :authentication_token, :unique => true
end
end
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MNwIeldQzcBbakbcLkbcIlNYiKQ72F3nJyqHOsDdRoM=",
"user"=>{"email"=>"email#example.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up",
"locale"=>"en"}
Devise Version:
2.1.2
Ruby version:
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
Rails version:
Rails 3.2.13

cant mass assign the protected attributes

iam working in rails 3.while trying to creating a user i am getting
cant mass assign the protected attributes error
I included following gems in the gemfile
gem 'authlogic'
gem 'gemcutter'
and run bundle install in rails console
then create a a user model and add the required authlogic columns to the migration.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :login, :null => false
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
t.string :persistence_token, :null => false
t.timestamps
end
end
end
and did rake db:migrate
Included authlogic in the user model.
# /app/models/user.rb
class User < ActiveRecord::Base
acts_as_authentic
end
while trying to create a user in rails console User.create(name: "pria",password: "priya123", password_confirmation: "priya123")
iam getting
cant mass assign the protected attributes :name, :password, :password_confirmation
How can i rectify this error!
In your User model:
attr_accessible :name, :password, :password_confirmation
You must add these attributes to the attr_accessible list in your model.
For important information about mass-assignment and its security implications: http://guides.rubyonrails.org/security.html#mass-assignment

Ruby on Rails Has_Many :Through Association

I'm having some problems with ruby on rails, specifically setting up a many-to-many connection with deal and event through deal_event. I've checked out several similar stackoverflow questions and even http://guides.rubyonrails.org/ but I'm still not getting something..
Here are my models:
deal.rb
class Deal < ActiveRecord::Base
has_many :deal_events
has_many :events, :through => "deal_events"
attr_accessible :approved, :available, :cents_amount, :dollar_amount, :participants, :type
end
event.rb
class Event < ActiveRecord::Base
has_many :deal_events
has_many :deals, :through => "deal_events"
attr_accessible :day, :image, :description, :location, :title, :venue, :remove_image
end
deal_event.rb
class DealEvent < ActiveRecord::Base
belongs_to :deal
belongs_to :event
end
And here are my migration files:
20130102150011_create_events.rb
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :title, :null => false
t.string :venue
t.string :location
t.text :description
t.date :day
t.timestamps
end
end
end
20130112182824_create_deals.rb
class CreateDeals < ActiveRecord::Migration
def change
create_table :deals do |t|
t.integer :dollar_amount
t.integer :cents_amount
t.integer :participants
t.string :type, :default => "Deal"
t.integer :available
t.string :approved
t.timestamps
end
end
end
20130114222309_create_deal_events.rb
class CreateDealEvents < ActiveRecord::Migration
def change
create_table :deal_events do |t|
t.integer :deal_id, :null => false
t.integer :event_id, :null => false
t.timestamps
end
end
end
After I seed my database with one deal and one event, I go into the console and type in
deal = Deal.first # ok
event = Event.first # ok
DealEvent.create(:deal => deal, :event => event) # Error: ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: deal, event
deal.events # Error: ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "deal_events" in model Deal
Any idea on what I'm doing wrong for these two errors to be popping up? Thanks.
You'll need this line in your DealEvent model:
attr_accessible :deal, :event
Although if it's just a relationship table (which it looks like) then you don't create the relationship that way. Use nested forms and the like.

Polymorphic many-to-many association error in Ruby On Rails 3

I am struggling with a polymorphic many-to-many association in rails 3.
My Article model looks like this:
class Article < ActiveRecord::Base
has_many :article_tags
has_many :people, :through => :article_tags, :source => :taggable, :source_type => :person
end
My ArticleTag model looks like this:
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :taggable, :polymorphic => true
end
My Person model looks like this:
class Person < ActiveRecord::Base
has_many :article_tags, :as => :taggable
end
Finally, my Organization model looks like this:
class Organization < ActiveRecord::Base
has_many :article_tags, :as => :taggable
end
I have a schema which looks like this:
ActiveRecord::Schema.define(:version => 20110322234836) do
create_table "article_tags", :force => true do |t|
t.integer "taggable_id"
t.string "taggable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "articles", :force => true do |t|
t.string "title"
t.text "content"
t.date "published_on"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "people", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "organizations", :force => true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I was hoping that this would allow me to create people and organizations relationships from my articles like this:
Article.create!
Article.first.people.create!
and hopefully to be able to access it the other way afterwards by
Person.first.articles
Unfortunately I get an error when I try to add a person this way:
Article.first.people.create!
NameError: uninitialized constant Article::person
.../base.rb:1199:in `compute_type'
.../reflection.rb:162:in `klass'
.../association_collection.rb:157:in `transaction'
.../has_many_through_association.rb:41:in `create_record'
.../has_many_through_association.rb:13:in `create!
Any help would be very much appreciated I've tried many alternatives but with no success.
I actually just had to some something similar to this recently. It looks like most of your code is right although in mine for the first section I would try changing do this:
class Article < ActiveRecord::Base
has_many :article_tags
has_many :people, :through => :article_tags, :source => :taggable, :source_type => "Person"
end
Also you can't do: Article.first.people.create({:name => "Tim"})
You would have to assign it to a variable first:
article = Article.first
article.people.create({:name => "Tim"})
Let me know if this works for you. I just skimmed through. If it doesn't i can double check my code again and see if there are any other differences.