Rails Forms for Private Messaging

徘徊边缘 提交于 2019-12-23 03:36:13

问题


I am trying to create a private messaging feature on my app between two users and I am running into trouble here

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 to start a conversation:

<%= 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 %> 

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)

  1. Is there a way to handle attributes from two different models on a single form without using Nested Attributes?

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

  1. Only workaround I could possibly think of is to add_content_to_conversation as well and pass it onto Message when Conversation is created for the first time. This is all I could think of with this db design. Is this inherently flawed?

Any help would be great. I am a rails beginner.

Thanks


回答1:


I'm having trouble following along (Long day) but does this help?

Messages

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


来源:https://stackoverflow.com/questions/10220467/rails-forms-for-private-messaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!