Search for a nested resource

63 views Asked by At

I have an event with many guests for that event. A guest must search themselves using their ID to RSVP but I cannot get the search to return that guests info (show/edit page).

The search tag:

<div>
  <h3>Enter your ID here:</h3>
  <%= form_for "", url: event_guest_path(@event,@guest), role: 'search', method: :get do %>
  <%= text_field_tag :search, @search_term, placeholder: "Search.." %>
  <% end %>
</div>

GuestController:

def show
    if params[:search]
      @search_term = params[:search]
      @guest = @event.guests.search_by(@search_term)
    end
  end

Guest.rb:

 def self.search_by(search_term)
    where("(id) LIKE :search_term", search_term: "%#{search_term}%")
  end

At the moment it keeps taking me to the index page but I want it to return the guest path where they can make their RSVP.

1

There are 1 answers

0
morissetcl On

We should have a more precision to clearly answer you. Because your query like a bit overkill.

A guest have a unique ID for a specific event ?

If that is the case I suggest you to simply use the following code in your show:

Guest.find_by(search_term: params[:search])

If your Guest has a unique ID which can be used for multiple event you probably want to create a join table but I am speculating.