I have a Threads controller and Messages controller.
Threads has_many Messages
Once a user clicks send, I send data into Threads controller to create the thread. I want to make it to so that
threads_controller.rb
def create
...
if @thread.save
#send data into messages_controller
#to create the corresponding table for a message with this thread_id
end
So, essentially I am trying to do two POSTS one after another if the first one succeeds.
I think redirect_to is what I am supposed to use. Is it possible to use redirect_to to pass params and call create action from a different controller?
EDIT:
I have to have a Thread(bad name choice for the reason Ryan mentioned, but for the sake of not confusing people with answers on the bottom, let’s keep it) Model and Message Model here. Thread table needs to only take in the message_title. The Message table takes in from_id(id of user sending message), to_id(id of user receiving message), and message_content. I am trying to do all of this in one form that takes in message_title and message_content.
I hope this helps in understanding the question.
Thanks everyone
I think you’re going about this in the wrong way.
First: I really hope that you’re not calling a model
Thread, as that would conflict with the Ruby classThread. If you are, choose a different word.Now with the “please aim the gun away from your foot” message out of the way…
You shouldn’t be calling out to the
MessagesControllerto create a new message for the controller. Instead, you should be using nested attributes in the new thread form:Inside your
DiscussionThread(I am assuming the name of it here) model, you would then have these lines:You may have to add
messages_attributesto theattr_accessibleattributes in this model too.This tells the
DiscussionThreadmodel that instances can accept attributes for themessagesassociation too. In yourThreadsControllerthe action would then remain the same.For more information about nested attributes, I recommend watching the Railscasts on Nested Forms #1 and Nested Forms #2.