Rails - How to use the 'has_secure_password' technique for emails - ruby-on-rails-3.1

Rails 3.1.0 - Ruby 1.8.6
I'm trying to create a signup page. I came across this 'has_secure_password' with basically does all the magic for you to enter your password twice and checks if they are correct.
I'd like to use this technique to check on email address as well. If it's not possible, what is the easiest way for me to get the user to key in their email twice and check if they match?
Please help. I'm a rails noob.
Thanks

Rails has a validates_confirmation_of validation built in. If your model has
class User < ActiveRecord::Base
attr_accessor :email_confirmation
validates_confirmation_of :email
end
Then you just need an email confirmation text field in your form and rails will check that they match.

Related

Validating a blank field Rails Model

This may be somewhat basic but cannot find a definitive answer anywhere. I have set up a contact form within my app and have put in a hidden field that when completed disables the submit button with some Jquery. My attempt at stopping automated spam..
Can I also add some validations in my model?
validates :ghost, :presence => false
Looking at the docs this is invalid? I want the form to fail if this field is filled in. Not sure how to go about this one
EDIT
So I have now read that I could possibly use
validates_exclusion_of :ghost, :on => :create
Though this is still failing as i dont think i am passing the correct arguments.
:presence => false means that you disable presence validator.
You need to write own absence validation (though in Rails 4.0 such validation exists, absence: true).
validate :ghost_is_absent
def ghost_is_absent
errors.add :ghost if ghost.present?
end
I am sorry to say , but why are you trying to do things so differently, doing it this way will make things more confusing for any future developer working on this piece of validation.
First thing:
1) You can do the reverse of it , mark it as spam when the field is empty and vice versa and then simply check with the validation validates_presence_of :ghost
2)or if you want to protect spam use capcha (recapcha gem for that)
3) or if you want it do it your way only , then just add a custom validation
Try creating a custom validation.
validate :check_for_spam
def check_for_spam
errors.add_to_base "ghost is present this is a spam" if ghost.present?
end
If you want to check if :ghost is blank:
validates :ghost, inclusion: {in: ['']}
If you want to check if :ghost is nil, you have to rely to a custom validator.

Mongoid has_many relationship causes Rack cookie error in Sinatra

Writing an application using Mongoid 3.1 and Sinatra in Ruby 1.9.3. I have a model called Order that has_many Items. Whenever I try to append an Item to an Order.items, I run into problems. I have the following route, summed up slightly:
order = session[:user].get_order(Time.now)
order.items << Item.new
order.save
"Hi, mom!" # Garbage page so that I know nothing else is called.
Doing that once is okay; doing it twice causes the following error:
Warning! Rack::Session::Cookie data size exceeds 4K.
Warning! Rack::Session::Cookie failed to save session. Content dropped.
I've been banging my head against the wall trying to get it to stop doing this. Why is the session loading all my items? Am I not using the has_many relationship correctly?
Your User model probably has_many :orders. Ruby is probably calling Marshal.dump to dump your user object into the cookie. You can imagine this might get huge. You should do the following:
Only store the user_id in the session.
Store your session server-side instead of in the cookie.
You'll need to use different middleware to store your session server-side. See this page for an example of storing your session in memcache. Since you're already using mongo, you could use Rack::Session::Mongo.
Even though you're not using Rails, the Rails guide on session security is useful reading. [link]

How can I work with attributes of a belongs_to relationship in Rails 3.1?

I have a couple of basic Rails problems that I'm having trouble finding relevant current (Rails 3+) information on. Here is the first:
How do I access attributes of a parent object to display them in the view? I have the following models:
class Site < ActiveRecord::Base
has_many :devices
class Device < ActiveRecord::Base
belongs_to :site
I'm using regular restful routes (do nested resources come into play here?) and the standard find methods in the Devices controller. In the view for devices I want to display the name of the site that owns the device, but everything I try gives me errors. How can I access and display the Site.name value for a given device?
Thanks in advance for your help!
It's hard to know what's wrong without seeing what you're trying. Compare the working example below with your technique. But first check that your device actually has a site, as explored here: Rails belongs_to association, can't access owner's attributes when part of a collection?
Try this in your rails console:
site = Site.create(:name => "Boston")
device = Device.create(:name => "hackatron")
site.devices << device
device.site.name #=> "Boston"
You can see my full output in this gist
If that doesn't help pinpoint where your error is, please share some code and the errors you're seeing.

Rails - how to update model without required items

I have a model that contains required and free fields. Between the required items is also item with password.
In the _form.html.erb I hid the input with a password, so when the user want to update the form, so he will be update the all items except password => that's what I want. But because the item password is require, so I will get an error about bad validations.
I would like to ask you, if exist a good way to do this... I can't find some trick for it
I think it's :
validates_presence_of :password, :on => :create

Data modelling of group and users

I have a web app that lets users create groups.
On the front page of the app any user can - without registering - create a group and add other users to the group by entering names into a list of text inputs. The user that created the group is then supposed to be able to invite the people on the list by adding an email to each of the names. The reason I want the user to provide just names initially and ivitations by email later is that I want the user to be able to try out the app and its functionality before registration.
So I have two models Group and User with a many-to-many relationship. The user model requires a unique email address so obviously I can't create new user objects for each user in the group. So how would one make sure that the temporary users added to the groups will be mapped to the users that are created through the invitation or if the invited person already has a user in the app?
I was thinking about creating a Profile model that would have a name and belong to a group and a user but that way each user would have a profile for each of the groups he/she belongs to. I can't think of another alternative but there must be one...
You need tools to dump dat info from your brain to a database model :)
http://wb.mysql.com/ or http://www.sequelpro.com/
Then you need to abstract that into your application.
http://datamapper.org/
After install all datamapper gems
gem install dm-core dm-do-adapter dm-migrations dm-sqlite-adapter dm-timestamps do_sqlite3
And finally you can start playing. In your Sinatra app.
require 'rubygems'
require 'dm-core' # http://datamapper.org/getting-started
configure :development do
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/hohohodb.db")
end
#...
class User
include DataMapper::Resource
property :id, Serial # http://datamapper.org/docs/properties
property :username, String
property :email, String
property :created_at, DateTime
property :updated_at, DateTime
validates_uniqueness_of, :email # http://datamapper.org/docs/validations
has n, :groups
end
# Create or Upgrade all tables at once
DataMapper.auto_upgrade!
get '/'
erb :index
end
__END__
##index
...
Hope this can be a good starting point # http://datamapper.org/docs/
have fun

Resources