Adding Summary Page to YelpDemo

After completing the YelpDemo course and playing around, I think it would be cool to add a reviews summary page to display as the home page rather than just seeing a list of restaurants. When a person visits the website they see a list of recent reviews and the restaurant associated with the review, kind of similar to yelp.com. The problem is, I honestly have no clue how to go about doing this. I know that we deleted the reviews index page in the project, but I wouldn't think that would work anyway because it doesn't show the restaurant associated with the review. I was wondering if someone could point me in the right direction? Any advise would be greatly appreciated! Thanks, Alan

Posted about 5 years ago by Alan Springle
Posted about 5 years ago by Alex Yang

Great question, Alan! Here are the steps to take if you want to set a reviews index page as the home page:

1) Add back the 'index' action to our reviews_controller.rb. You should be able to see the original code in the video when it was deleted. Make sure to also tweak the before_action's at the top so that our user permissions are set the way we'd like!

2) Create an index.html.erb View page under the 'reviews' folder. This is the page that we can customize to display whatever information we'd like, including the Restaurant associated with the Review. For example, if we have a review contained in the variable '@review', we can access the associated restaurant with '@review.restaurant'.

3) Set the reviews index page as the home page by changing "root 'restaurants#index'" to "root 'reviews#index'".

This 3-step process can be generalized for any case where you need to add a new page and set it as the home page. Hope this helps!

0
Posted about 5 years ago by Alan Springle

Awesome, thanks for the help Alex. I created a new index file, and have the root pointed to the reviews index. Between playing with the controller and view page, I now have restaurants with corresponding reviews showing. Is there an easy and basic way to automatically generate a heading with the date and then list the reviews and associated restaurant entered on that particular day?

I have the reviews and related restaurant listed in descending order in a table currently. I was thinking that if the reviews were sorted out by date with a generated header, it would be easier for the visitor to process at a glance. So for example, <h2>Generated DATE</h2> and then the reviews for that date listed below.

Thanks again!
Alan

0
Posted about 5 years ago by Alex Yang

Unfortunately, there isn't a way to do this automatically, but it won't be too difficult. For features like this, the best approach is to boil it down to the basics and then build it up layer by layer. In this case, see if you can set it up so that it displays the date for each review in order with most recent first. Then, see if you can remove the duplicate dates. Try it yourself and I'll help out if you get stuck.

0
Posted about 5 years ago by Alan Springle

Hi Alex, thanks for advise. I now have the view displaying the reviews with the most recent date first. For the life of me, I can't figure out how to show the date on the first review and then strip the date from the other reviews entered on the same day. I've been working on it for a few hours now and can't figure it out. I read on rubyonrails.org about using the .uniq attribute. I tried the example they listed, and didn't get an error on my view page but got a really strange result instead of the date.

Thanks!
Alan

0
Posted about 5 years ago by Alex Yang

The harder part is definitely removing the duplicate dates. Try this approach:

As you loop through the reviews, keep track of the previous review's date using a variable (yes, you can define new variables in your view page!). If the current review's date is the same as the previous review's date, then don't display the date again.

0
Posted about 5 years ago by Alan Springle

Hi Alex, I think that I'm going to have to come back to dropping the dates. I've spent several more hours trying to get things working, so I think this is something that I'll have to revisit at a later date when I learn more. It's driving me crazy and not really an important feature in the scope of things. Thanks for the help Alex.

On thing I did notice though, after I pushed changes to Heroku, the reviews page will not render and gets an error. Things work great in my dev environment (nitrious.io), but on Heroku I get the error ActionView::Template::Error (undefined method `restaurant' for nil:NilClass). Any ideas as to what might be causing the error? It's just very puzzling to me that everything is working fine in the dev environment but won't even load on Heroku.

Thanks again for your time!
Alan

0
Posted about 5 years ago by Alex Yang

No problem. When you come back to it, let me know if I can help.

You'll see the 'undefined method...for nil:NilClass' whenever your variables aren't being set properly. For example, if you use '@review.restaurant' in your View page, it'll work as long as '@review' was set to a Review object. If instead your app couldn't find the Review object, then '@review' will be set to nil and that's why you'll see this error.

The solution is to understand why the variable isn't being set properly. You can do this with debugging statements, like 'puts "The value of the @review variable is #{@review.inspect}"'. Using statements like these in your controller file will allow you to check your Rails server (or Heroku logs) to see what the output is.

0
Posted about 5 years ago by Alan Springle

Hi Alex, thanks again for your help I truly appreciate it! I followed your recommendation about debugging. I could not produce any meaningful results, most likely because I don't know what I'm doing! :) I've been fighting this issues for a day or so now, and it just didn't make sense that everything worked perfectly in the nitrous.io environment, but not on heroku. I looked at the tables in heroku and everything seemed normal. I then checked to make sure that all migrations where present, and executed. Once again, everything seemed normal.

As a last ditch effort, I decided to blow away the heroku database and recreate it. I ran heroku pg:reset DATABASE, which essentially dropped the whole database. I then then ran heroku run db:migrate which executed all of my migrations again and recreated the database. I created a new account, new restaurant, and a new review. The review now appeared on my summary page!

I do not know what the problem was with my database. Obviously if this were in production and people were using this app, I could not do what I did. I'd probably have to resort to hiring someone who is much more knowledgeable than myself to correct the issue. The joys of learning!

Thanks again for you help!
Alan

0
Posted about 5 years ago by Alex Yang

It was probably a database-related issue then. As you gain more and more experience with Rails, it'll become easier to be able to diagnose and troubleshoot problems. For these sort of database issues, it's generally quick to fix - the hard part is being able to figure out the root cause!

I'll add a 'Rails troubleshooting guide' to my content queue - hopefully that will be helpful to you (and others) in understanding the process/techniques I use in diagnosing and fixing Rails errors.

0