I’m building a private message system (using SO code) on top of a forum system. When a new message is added to a private message conversation the body is blank but the time-stamp and user is correct.
The system works as follows. A conversation is built with a first post and recipients. This works as it was copied verbatim from SO question 8205284.
Now I’m attempting to add a new message to the conversation through the edit/update actions in the user_conversation controller (which is the controller that builds a conversation).
Routes:
resources :users do
resources :conversations, :controller => "user_conversations"
end
resources :conversations, :controller => "user_conversations" do
resources :messages
end
Controller:
def edit
@conversation = UserConversation.find(params[:id])
@message = Message.new
end
def update
@conversation = UserConversation.find(params[:id])
@message = @conversation.messages.build(params[:message])
@message.user = current_user
@message.conversation_id = @conversation.conversation_id
@message.body = "without this test line the message body is blank!"
if @message.save!
redirect_to user_conversation_path(current_user, @conversation)
else
redirect_to @conversation
end
end
View to show conversation and embedded new message form:
<%= form_for(@conversation) do |c| %>
<div class="field">
<%= c.fields_for :messages do |m| %>
<%= m.text_area :body, placeholder: "New message..." %>
<%= m.submit "New Convo Message", class: "btn btn-large btn-primary" %>
<% end %>
</div>
<%= c.submit "New Convo Message", class: "btn btn-large btn-primary" %>
<% end %>
Log output:
Processing by UserConversationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"InYNwRo47ec6meePrKci3z95yTgKC4K7ytO8wFHuFns=", "user_conversation"=>{"messages"=>{"body"=>"testing"}}, "commit"=>"New Convo Message", "user_id"=>"18", "id"=>"18"}
I think you need:
because messages are nested in user_conversion based on your params.
messages corresponds with: