class PrivateMessage < ActiveRecord::Base
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
belongs_to :recipient, :class_name => "User", :foreign_key => "recipient_id"
end
Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts
Tuesday, September 25, 2007
Tutorial: Foreign Keys with Different Names
I had to make private messages that had a sender_id and a recipient_id that both referred to the user table. Here's how the model should look:
Tutorial: Migration Table w/o IDs
When creating HABTM(Has And Belongs To Many) tables, you can create the migration table without an ID column this way:
create_table :events_users, :id => false do |t|
t.column :event_id, :integer
t.column :user_id, :integer
end
Tutorial: Fixing file_column's permissions
I came across a problem with file_column because it didn't update the permissions of the files and folders where you'd upload.
They'd stay at 600. I'd come across a routing error when trying to download files.
In the file_column.rb I edited the move_from method to look like this:
Since posting this, we've moved to attachment_fu. It's a lot nicer, but has a steeper learning curve. If you're learning on how to attach files, file_column is fine.
They'd stay at 600. I'd come across a routing error when trying to download files.
In the file_column.rb I edited the move_from method to look like this:
def move_from(local_dir, just_uploaded)
# create a directory named after the primary key, first
unless File.exists?(@dir)
FileUtils.mkdir(@dir)
#sets the permissions just in case it hasn't been set.
FileUtils.chmod_R 0755, @dir
end
# move the temporary files over
FileUtils.cp Dir.glob(File.join(local_dir, "*")), @dir
@just_uploaded = just_uploaded
# remove all old files in the directory
FileUtils.rm(Dir.glob(File.join(@dir, "*")).reject! {
|e| File.exists?(File.join(local_dir, File.basename(e)))
})
end
Since posting this, we've moved to attachment_fu. It's a lot nicer, but has a steeper learning curve. If you're learning on how to attach files, file_column is fine.
Tutorial: Email Authentication When Signing Up
This is to help us remember how to do email authentication (emailing the password to the user, so that they can use that to log in) when signing up.
This assumes you're using the LoginGenerator.
1. In account_controller.rb, add this as a private method:
2. Then add/edit this method to the account_controller.rb
3. Make the notifier:
4. Make the method that takes care of the emailing in notifier.rb:
5. Create views/notifier/account_details.rhtml like this:
6. Finally, add this to the bottom of config/environment.rb:
This assumes you're using the LoginGenerator.
1. In account_controller.rb, add this as a private method:
def generate_password(length = 8)
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('1'..'9').to_a - ['o', 'O', 'i', 'I']
Array.new(length) { chars[rand(chars.size)] }.join
end
2. Then add/edit this method to the account_controller.rb
def signup
case @request.method
when :post
@password = generate_password
@user = User.new(@params['user'])
@user.password = @password
if @user.save
#@session['user'] = User.authenticate(@user.login, @params['user']['password'])
Notifier::deliver_account_details(@user, @password)
flash['notice'] = "An email has been sent."
redirect_to :action => "login"
end
when :get
@user = User.new
end
end
3. Make the notifier:
ruby script/generate model Notifier
4. Make the method that takes care of the emailing in notifier.rb:
class Notifier < ActionMailer::Base
def account_details(user,password)
# Email header info MUST be added here
@recipients = user.email
@from = "noreply@domain.com"
@subject = "Account Details"
# Email body substitutions go here
@body["username"] = user.login
@body["password"] = password
end
end
5. Create views/notifier/account_details.rhtml like this:
Hello <%= @username %>,
Your password is:
<%= @password %>
When you log in, please change your password for security.
Thank you,
Mailman
6. Finally, add this to the bottom of config/environment.rb:
ActionMailer::Base.smtp_settings = {
:address => "mail.domain.com",
:port => 25, #some servers use 26 - I got stuck here before!
:domain => 'www.domain.com',
:user_name => "info@domain.com",
:password => "pass",
:authentication => :login
}
Subscribe to:
Posts (Atom)