0

Just started coding in Ruby on Rails and have managed to create the basic CRUD functionality for my app.

I can also list them all. Now I would like to create a filter for the user to interact with.

Database Schema
create_table "nades", force: :cascade do |t|
   t.string   "title"
   t.integer   "grenade"
   t.text     "description"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.integer  "map_id"
end

The "grenade" can have a value from 1-4, corresponding to a specific grenade:

[[1,"smoke"], [2,"flash"], [3,"molotov"], [4,"he-grande"]

Now I'm trying to create a filter with 4 buttons in the view. Where you can toggle each nade on/off to show or hide them in the results.

[x]Smoke [ ]Flash [ ]Moltov [x]HE

This should only return the nades where grenade = [1,4]

After some reading it looks like scoped would be nice to use to manage this. However I'm not sure how to make it work as I want.

Was thinking of doing something like:

scope :by_nade, -> grenade { where(grenade: grenade) if status.grenade? }

However this only allows me to get 1 specific nade type from the database. Is it posible to send multiple parameters like:

http://localhost:3000/nades/?by_nade=1,2 ??

Or is it a better solution to my problem?

1 Answer 1

0

Look into creating a form with an array of options (e.g., here). That will allow you to get multiple values for the specific type of grenade.

Your scope will work with either a single id or an array of ids and using the array of options approach should yield an array that will work. In your example above it would effectively be Nade.by_grenade([1,4]). You might want to guard against the array being empty (assuming that an empty list would be a bad thing).

Sign up to request clarification or add additional context in comments.

2 Comments

I gues I would have to create a model for the 'grenade' to make this form?
Figuered out how to make the form with the option as an array :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.