Question

I am using the mailboxes gem in rails 4. I am trying to make it so that when A user goes to another user's profile, they click on a button on that profile that sends the second user a message. For example, A goes to B's profile and clicks the message button. B will then get a message from a. On the profile page, I have the following code:

<%=current_user.send_message(@user, "Body", "subject") %>

I've also tried placing the code above in a link_to tag but that does not work. On the Receive messages page, I have the following code that I got from the documentation:

Conversations:

<%= current_user.mailbox.conversations %>


Inbox:

<%= current_user.mailbox.inbox %>

But nothing appears in either the inbox code or the conversations code. Please help. Thanks.

Was it helpful?

Solution

If you want your code to be evaluated use <%. <%= is for writing stuff out on the page. Also the .send_message method really shouldn't be in your view. Create an action in your controller, or create whole service object and make it responsible for messaging.

One thing is that code like this really doesn't belong in a view. But more importanlty, you should be calling it, only if user click a link. So, simple solution would be:

  1. create an action 'send_message' in one of your controllers
  2. create a route for this action
  3. put the line current_user.send_message(@user, "Body", "subject") into your newly created action, use redirect if you don't want a template for this action
  4. and in your view add a link (or button), to this action
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top