def new
@title = 'new diary entry'
if params[:diary_entry]
- @entry = DiaryEntry.new(params[:diary_entry])
- @entry.user = @user
- if @entry.save
+ @diary_entry = DiaryEntry.new(params[:diary_entry])
+ @diary_entry.user = @user
+ if @diary_entry.save
redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name
end
end
def new
@title = 'send message'
if params[:message]
- to_user = User.find(params[:user_id])
- body = params[:message][:body]
- title = params[:message][:title]
- message = Message.new
- message.body = body
- message.title = title
- message.to_user_id = params[:user_id]
- message.from_user_id = @user.id
- message.sent_on = Time.now
+ @message = Message.new(params[:message])
+ @message.to_user_id = params[:user_id]
+ @message.from_user_id = @user.id
+ @message.sent_on = Time.now
- if message.save
+ if @message.save
flash[:notice] = 'Message sent'
redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
- else
- @message.errors.add("Sending message failed")
end
-
end
end
class DiaryEntry < ActiveRecord::Base
belongs_to :user
+
+ validates_presence_of :title, :body
+ validates_numericality_of :latitude, :allow_nil => true
+ validates_numericality_of :longitude, :allow_nil => true
+ validates_associated :user
end
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id
belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id
+
+ validates_presence_of :title, :body, :sent_on
+ validates_inclusion_of :message_read, :in => [ true, false ]
+ validates_associated :sender, :recipient
end