Private messages: how to REST-ify reply/preview/draft controller actions?
I'm working on a simple message system for a project. Trying to stick to REST guidelines.
I have the following models:
class Member << AR::Base
has_many :sent_messages, :class => 'Message', :foreign_key => :sender_id
has_many :received_messages, :class => 'Message', :foreign_key => :recipient_id
end
开发者_运维问答
class Message << AR::Base
belongs_to :sender, :class_name => 'Member', :foreign_key => :sender_id
belongs_to :recipient, , :class_name => 'Member', :foreign_key => :recipient_id
end
Now I have MessagesController defined with 7 RESTful methods. I'm able to send a new message by using new (to render form) and create (to actually send msg) methods.
Questions:
- How to handle reply functionality with REST? I though of adding another method called reply, which will function much like new() to render form and would also submit to create(). Is there a better way?
- Message preview functionality? Add another action? New controller?
- How to handle saving a draft message (no recipient_id specified)? Should I be reusing create method in MessagesController?
Open to any suggestions. Thanks in advance.
The draft is just a message, which has not been sent yet, and it's possible that it is not completed.
I suggest you add a boolean attribute 'sent' to the message. If you save the object with 'sent=false' then you run only partial validations (if any at all). This way, you have handled the 'saving draft' problem.
If you save the message with 'sent=true', then you run the full validation, save the object in the database and actually send it.
Don't think about the 'create' method as just other word equal to 'send'. You are sending the message when you save it in a state allowing it to be sent. It does not matter whether the user marks the message as ready when creating it or when updating it. Handle it at the model level.
How does the 'preview' differ in functionality with just reading the message? The standard GET /messages/1
should be OK.
About reply. What is the replying? It's just creating a new message with some fields predefined. Make your :new
action recognize additional parameters, and present a link on all related views, like this:
<a href="/messages/new?to=john@example.com&in-reply-to=<1234...>">Reply</a>
精彩评论