On the product listing page of my marketplace, I've create a button the send a message to the user of each product, with the link_to:
<%= link_to "Contact", new_conversation_path(recipient_id: product.user.id) %>
Here is my converations_controller:
class ConversationsController < ApplicationController
before_action :authenticate_user!
def index
@conversations = current_user.mailbox.conversations
end
def show
@conversation = current_user.mailbox.conversations.find(params[:id])
end
def new
@recipient = User.find(params[:recipient_id])
end
def create
recipient = User.find(params[:recipient_id]).id
receipt = current_user.send_message(@recipient, params[:body], params[:subject])
redirect_to conversation_path(receipt.conversation)
end
end
My new conversation page:
<h1>New conversation</h1>
<%= form_tag conversations_path, method: :post do %>
<div>
</div>
<div>
<%= text_field_tag :subject, nil, placeholder: "Subject" %>
</div>
<div>
<%= text_area_tag :body, nil, placeholder: "Your message" %>
</div>
<%= submit_tag "Send",:class=>"btn btn-warning" %>
<% end %>
When I click on the link to send a message I am correctly redirect to the "conversation new" page, which has on its URL:
/conversations/new?recipient_id=7
With the id of the correct user id, but when I send the message,
I have the following error:
couldn't find user with 'id'=
I make it working finally, here is my code, maybe it can help other:
(i'm on rails 5)
The link on my product page:
<%= link_to "Contactar", new_conversation_path(recipient_id: service.user.id) %>
My new and create action on my conversation controller:
def new
@recipients = User.find(params[:recipient_id])
end
def create
recipient = User.find_by(id: params[:recipient_id])
receipt = current_user.send_message(recipient, params[:body], params[:subject])
redirect_to conversation_path(receipt.conversation)
end
On the new conversation:
<%= hidden_field_tag(:recipient_id, "#{@recipients.id}") %>