用于私人消息的Rails表单 [英] Rails Forms for Private Messaging
问题描述
我正在尝试在我的应用程序上创建两个用户之间的私人消息传递功能,我在这里遇到麻烦
I am trying to create a private messaging feature on my app between two users and I am running into trouble here
首先,这里是Schema.rb
First, here's Schema.rb
create_table "conversations", :force => true do |t|
t.string "conversation_subject
end
create_table "messages", :force => true do |t|
t.string "content"
t.integer "user_id"
t.integer "conversation_id"
end
create_table "participants", :force => true do |t|
t.integer "conversation_id"
t.integer "user_id"
end
conversations has_many :messages, :participants
users has_many :messages, :participants
开始对话的表格:
<%= form_for @conversation do |f| %>
<div class="field">
<strong>Subject</strong><br />
<%= f.text_field :conversation_subject %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
在上面的表格中,我想拥有
In the form above, I wanted to have
<%=f.text_area :content %>
也适用于Message,因此只需单击一下即可创建对话"和"Message",但由于user_id,我无法在此处使用嵌套属性(也许
你可以?但据我了解,你不能这样做
for Message as well so that with one click it creates Conversation and Message, but I couldn't use Nested Attributes here due to user_id (maybe
you can? but from my understanding you can't)
- 是否有一种方法可以在不使用嵌套属性的情况下以单一形式处理来自两个不同模型的属性?
注释:我知道user_id可以从Message中删除,但从理论上讲它仍然具有正常的消息传递系统,但是我认为为了将每个消息与发件人相关联,包含
note I know user_id could be taken out of Message and still have a functional messaging system theoretically, but I thought in order to associate each message with the sender, it'd be necessary to have it included
- 我可能唯一想到的解决方法是,在首次创建会话时,也将add_content_to_conversation传递给Message.这就是我可以想到的所有数据库设计.这是天生的缺陷吗?
任何帮助都会很棒.我是Rails初学者.
Any help would be great. I am a rails beginner.
谢谢
推荐答案
(很长一段时间)我在跟踪时遇到了麻烦,但这有帮助吗?
I'm having trouble following along (Long day) but does this help?
消息
belongs_to :recipient, :classname => 'User'
belongs_to :sender, :classname => 'User'
create_table "messages", :force => true do |t|
t.string "content"
t.integer "recipient_id"
t.integer "sender_id"
t.integer "conversation_id"
end
这篇关于用于私人消息的Rails表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!