create_stories
, next added a new field t.string:author
in my migration file, and then ran the migration, what does it to do my table?Let's walk through building our own full CRUD app, but instead of Twitter, we'll do it for Reddit—without the API.
Remember the steps we did in class, and think about what lines of code go with each step:
reddit_by_hand
Like mentioned in lecture, it doesn't really matter what you create first: routes, models, migrations, controllers, or views. But at the infancy of your coding career, errors help guide you step-by-step in decide what to build. So if you see uninitialized constant StoriesController
, try solving that. Then you'll get another error, so solve that. And so on.
Without data or models:
With data and models:
Step 4 might look something like:
# In the controller
def action_name
@models = Model.get_stuff
end
# In the view
# ...show @models in some way
Go through the above steps (based off what we did in class) to create an app called reddit_by_hand
. Give it functionality so that, using the application via the web browser, you can view all stories, create a new story and show a single story (this is new!).
BEFORE YOU START, think:
This is why MVC is called MVC (but maybe should be called MVCr to incorporate routes). It's a way of structurally thinking about your application in an organized, step-by-step way. Hopefully things are slowly coming into focus!
Only now that you've designed your application, you should start building it. Godspeed.
If you didn't in the exercise above, create a User
model and a Category
model, and migrations for each of them. What attributes will each have and what are their types? Give each Story
a user_id
and a category_id
. Make sure to create a new migration, don't edit the one you already created!
In your seeds.rb
file, create a user, a category, and then 5 stories that belong to that category and user. Edit your view to show the user_id
and category_id
of each story.