Show random restaurant name with a click of a button

Hi! So I just finished the Yelp course and I am trying to build one with a little tweak. Here is my halfway done app http://eatwhatlater.herokuapp.com I managed to add a spinning wheel, but what I would like to ask is, what should I do so that when I click the spin button, the restaurant info on top of the wheel will shuffle and return a random restaurant name from the db list? I tried to google search and got this gem install https://github.com/grosser/random_records but isn't sure how to actually implement it. Thanks in advance!

Posted about 4 years ago by Anthony
Posted about 4 years ago by Alex Yang

I don't think you need to install a gem to get this functionality to work. You'll need to use the 'rand' method to draw a random number. For example, 'rand(3)' will give you 0, 1, or 2. If you need 1, 2, or 3, then just use '1 + rand(3)'. So the methodology you need is to count the number of restaurants in your database, put them all into an array, and pick the nth one. Since you won't be refreshing the page, you'll need to use JavaScript/AJAX to get this to work though!

1
Posted about 4 years ago by Anthony

Hi Alex, thanks for the response. will this work?
@restaurants = Restaurant.order('RANDOM()').limit(1)

When I try it returns this instead #<Restaurant::ActiveRecord_Relation:0x007fd7a2bdceb0>

Still pretty new, not really sure how to count the number and put into array.

THanks!

1
Posted about 4 years ago by Alex Yang

Hmm...I'm worried that setting up this feature may be a little too advanced for you at this point. Even after you understand how to draw a random restaurant from your database, you will still need to get it to work using AJAX so the user doesn't need to refresh the page. It's a lot for someone just starting out to pick up, so I would recommend saving this feature for later once you've had a little more experience.

To be clear, my intention isn't to discourage you - it's actually the opposite. You'll learn the most when the challenges you choose are just beyond your current skill level, and I think this particular feature (since it requires you to use both advanced Rails and JavaScript concepts) may be too far beyond that sweet spot. Instead, perhaps you should consider building a different feature that will build on the Rails concepts that you've learned so far. Of course, if you still want to give it a shot, I'd recommend spending some time to learn JavaScript/AJAX and I'd be happy to talk you through this random restaurant piece as well.

1
Posted about 4 years ago by Anthony

Hi Alex,

thanks for your kind consideration. I still wish to give it a shot, to make this work. Currently, I manage to make it so that when user clicks the spin button, the name will change. However, I am using a hard coded variable like this

var rdm = ["ABC cafe", "Funny Cafe", "Test Cafe"]

Is there an easy way to access my ruby database and push them into a variable in javascript?

Thanks

1
Posted about 4 years ago by Alex Yang

To retrieve a random restaurant from your database, you can do something like this from your controller:

@random_restaurant = Restaurant.find(rand(Restaurant.count))

This first chooses a random number based on the number of entries in your Restaurant database. Then using that number, it retrieves the corresponding Restaurant.

You can push this variable to JavaScript using a gem like gon:
https://github.com/gazay/gon

1
Posted about 4 years ago by Anthony

Hi Alex,

I've tried the method and used the gem gon to push variable to Javascript. I think it works, but it returns [Object Object]. Google suggest using console.log() or console.dir() to return a string, however, it returns 'undefined' instead. Did I missed out something?

Thanks!

1
Posted about 4 years ago by Alex Yang

Instead, why don't you pass the elements of the restaurant you need to display directly? For example, to pass the name of the restaurant, you would use:

@random_restaurant = Restaurant.find(rand(Restaurant.count))
@random_restaurant_name = @random_restaurant.name

1