Add Pages To Create And Look At Individual Posts
Requirements to advance
- The user should be able to create a post with a title, author, date published, and content. The author should be the current user.
- The complete post should appear on its own page (aka its show page).
- If the user doesn't submit all required fields, they should see some error messaging, but shouldn't lose any of their work.
- How do you create a resource? What parameters do you need to pass?
- Then: what did rails make for you when you used the resource generator? Find all the new files. Maybe even list them out on a whiteboard.
- How do you associate a user with a post?
- What did you have to put in the migration to create posts?
- How is this association communicated to the User and Post models?
- How will you associate the current user with a post when you create it?
- What's the difference between the
createmethods in the posts controller and how do these relate to the form to create a new post?
Tools and References
- RailsGuides - Active Record Associations: http://guides.rubyonrails.org/association_basics.html#the-belongs_to-association.
- RailsGuides - Form Helpers, section 2.2: http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object
- RailsGuides - Routes - CRUD, Verbs, and Actions: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions.
- RailsGuides - Active Record Validations and Callbacks: http://guides.rubyonrails.org/active_record_validations_callbacks.html
- Rails has some built in ways to associate one model with another. See the RailsGuides link above for hints on how to make a user the owner of a post!
- Don't hand code the form! You don't have to! Rails will help. See RailsGuide link above!
- Rails has a built-in way to note when something was stored in the database. Probably handy for showing the date / time a post was created.
- For now, we're going to use the user's email address when displaying a post's author. You can add names or other identifiers later! (Also, even though you're going to get the current user's email address from the User model, you'll still need a user parameter for your Post resource.)
- You need a
createmethod to store your post data - scroll down a little bit in this section of the Getting Started guide for very helpful information and to see an example of a create method: http://guides.rubyonrails.org/getting_started.html#creating-new-posts.