简体   繁体   中英

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

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

self.where(user_id: user_pseudo)

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

##or Provide more details please

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM