0

I generated a devise model User, and I also have a Conversation controller. I'm showing all users, except the logged in one, and I'm trying to create a new Conversation between user1 and user2, but I get redirected to the index method of the Conversation Controller, not the create one. I understood from this link that making a post from one controller to another is a bad idea Rails: How to POST internally to another controller action?.

I've also tried to make a send_message method inside the Users controller and define it as a post in routes, but I get redirected to the show method of the Users controller.

What is the clean way of doing this?

class UsersController < ApplicationController
  before_action :authenticate_user!

  def index
    @users = User.where.not(id: current_user.id)
  end

  def send_message
    # @conversation = Conversation.new(conversation_params)
    # if @conversation.save
    #
    # end
  end

end

index.html.erb

    <div class="col-xs-12 col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1> User's index </h1>

<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <th>Email</th>
      <th>Created</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.email %></td>
        <td><%= time_ago_in_words(user.created_at) %> ago</td>
        <td>
          <div class="btn-group">
            <%= link_to 'Send', conversations_path(sender_id: current_user.id, recipient_id: user.id) %>
          </div>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>


</div>

Edit:

private
  def conversation_params
     params.require(:conversation).permit(:sender_id, :recipient_id)
  end


<ActionController::Parameters {"_method"=>"post", "authenticity_token"=>"394MDmcVVelccU//8ISYeqmk146exYc6G7SrrAhbCA/yQ/K8KTpSn/0EkXlZ4hB/g==", "recipient_id"=>"1", "sender_id"=>"3", "controller"=>"conversations", "action"=>"create"} permitted: false>
Community
  • 1
  • 1
Bogdan Daniel
  • 2,689
  • 11
  • 43
  • 76

2 Answers2

0

By default link_to helper sends GET request. You can do it with adding method: :post to its options.

<%= link_to 'Send', path, method: :post %>
Sergei Stralenia
  • 855
  • 7
  • 16
  • Thank you. You are right. No idea how I didn't think of that. But is this the right way to go? As I posted that link that says that I shouldn't pass params between controllers? Or is that referring to something else? – Bogdan Daniel Sep 12 '16 at 07:16
  • You don't need pass params between controllers :) You should send your request to ConversationsController#create action (conversations_path) – Sergei Stralenia Sep 12 '16 at 07:22
  • The params go to the create method, but my conversations_params.permit is complaining `param is missing or the value is empty: conversation`. I updated with the private method and with what I'm sending. Should I send a new conversation? – Bogdan Daniel Sep 12 '16 at 07:43
  • Yeah, it's reasonable because i suppose in your conversation controller you have something like: `params.require(:conversation).permit(...)`. So if you have this code it means that you have new conversation action, may be you should just redirect from users#index to conversations#new when you click 'Send'? Sorry, may be I'm wrong, it's difficult to understand your app remotely ;) – Sergei Stralenia Sep 12 '16 at 08:25
0

You could redirect to new_converstion_path instead of conversations_path. And link by default sends GET not POST request.

Anton
  • 540
  • 3
  • 13