Get the id of a User through a form or a search box on ruby

118 Views Asked by At

I am currently working on a Friendship feature. I would like to let the user select or type the name of another user in order to send a friend request.

Here is my form :

<%= form_tag users_path, method: 'get' do %>
 <%= label_tag(:search, "Who are your friends ?") %>
 <%= text_field_tag :search, params[:search] %>
 <%= link_to user_friendsips_path(), method: :post do %>
 <button class= "btn btn-user shadow"> <i class="fas fa-share-square"></i> Send Friend Request? </button>
 <% end %>
<% end %>

In the link_to where there is the button I would like to pass between the bracket the user_id the person selected in the form but I don't know how to retrieve it

Here is a part of my User Model

def self.search(search)
    if search
      user_pseudo = User.find_by(pseudo: search)
      if user_pseudo
        self.where(user_id: user_pseudo)
      else
        @users = User.all
      end
    else
      @users = User.all
    end
  end

Here is a part of my User controller with the Index

def index
@friends = current_user.friends
 @pending_requests = current_user.pending_requests
 @friend_requests = current_user.received_requests
 @users = User.search(params[:search])
end

Thanks by advance

1

There are 1 best solutions below

4
On
self.where(user_id: user_pseudo)

## I think you need to replace user_id with just id

##or Provide more details please