1 / 16

Ruby on Rails

Ruby on Rails. Your first app. So far we talked about…. DB migration Scaffolding Layouts Models Controllers … time to practice!. Add a second model. Create a Comment with properties: c ommenter: string body:text post:references Check db table: Run db migration.

gretel
Download Presentation

Ruby on Rails

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby on Rails Your first app

  2. So far we talked about… • DB migration • Scaffolding • Layouts • Models • Controllers • … time to practice!

  3. Add a second model • Create a Comment with properties: • commenter: string • body:text • post:references • Check db table: • Run db migration

  4. What did your command generate?

  5. Associating models • First check the Comment model • It belongs to a post • Then add the association to the Post model • A post can have many comments • Retrieve all comments using array: @post.comments • See more about Active record associations: http://guides.rubyonrails.org/association_basics.html

  6. Adding a route for comments • Remember Home controller?? • We need to add a link to navigate and see comments • Which file do we need to edit? • Add nested resource:

  7. Generate a controller • Now that we have a comments model, we will need to generate a controller • Use scaffold!

  8. Files created

  9. Still comments do not show… • Edit: /app/views/posts/show.html.erb

  10. Change the comments controller Still we do not see the comments we create..

  11. Showing comments • Which file do we need to edit??

  12. Refactoring • Make app/views/posts/show.html.erbsmaller • Hints: • Create the fileapp/views/comments/_comment.html.erb. Put comments show part into it. Add a render statement in show.html.erb • Create: app/views/comments/_form.html.erb. Add the proper render statement to show.html.erb

  13. Deleting comments • Edit: _comments.html.erb • Add the delete action to controller:

  14. Deleting associated objects • If you delete a post then its associated comments will also need to be deleted.

  15. Security • use the Rails http_basic_authenticate_with method

  16. Security

More Related