GET with one controller POST to another

38 views Asked by At

I'm trying to post to the messages controller from a form rendered by the conversations controller I'm passing the conversation_id by hidden_field_tag but I get the error ActiveRecord::RecordNotFound (Couldn't find Mailboxer::Conversation with 'id'={:value=>8}

Here's my form:

<%= form_tag reply_to_conversation_for_chat_view_path, id: "chat_message_form", method: :post, remote: true, multipart: true do %>
    <%= hidden_field_tag :recipients, value: @recipients.first.id %>
    <%= hidden_field_tag :sender, value: current_user.id %>
    <%= hidden_field_tag :subject, value: 'No subject' %>
    <%= hidden_field_tag :conversation_id, value: @conversation.id %>


<div class="field">
    <%= text_area_tag :body, nil, rows: 3, cols: 75 %>
</div>

<div class="field">
    <%= label_tag 'Attachment' %><br>
    <%= file_field_tag :attachment %>
</div>

<br>
    
    <div class="submit">
       <%= submit_tag 'Send' %>
    </div>
    
<% end %>

And here's the set_conversation routine in the messages controller:

      @conversation = current_user.mailbox.conversations.find(conversation_id)
2

There are 2 answers

0
fugee ohu On

I solved this by adding the conversation_id to the form definition

<%= form_tag reply_to_conversation_for_chat_view_path(@conversation.id), id: "chat_message_form", method: :post, remote: true, multipart: true do %>
1
arieljuod On

Your issue there is that you are using value: @conversation.id as the value, the actual key value pair. The second argument for hidden_field_tag is just the value:

= hidden_field_tag :conversation_id, @conversation.id

https://apidock.com/rails/v6.0.0/ActionView/Helpers/FormTagHelper/hidden_field_tag

That will give you a params[:conversation_id] with "8" instead of "{value: 8}".

You must have the same issue with the rest of the hidden fields.