0

So I have a form where in an input (type: text) and a select option one next to each other. I want to be able when choosing an option from the select option (either "search from your food" or "search food") to search from the corresponding table in the database. For example: You choose from the select option "search from your food" so in the text input you query the "user_food" table. If you choose "search food" you query the "food" table. Oh, by the way I use Sqlite3 as a database (the standard for Rails).

This is how the input looks so far:

enter image description here

And this is the code for it: enter image description here

Any help would be appreciated! Thanks in advance!

1 Answers1

1

View:

<%= select_tag :food_selection, options_for_select([['Search for food', 'foods'], ['Search your food', 'user_foods']]) %>

Controller

if params[:selection] == 'foods'
  @foods = Food.where('...')
else
  @foods = UserFood.where('...')
end

@foods = @foods.limit(20) # chain methods here they both share
Tallboy
  • 12,847
  • 13
  • 82
  • 173