and files. The ones we want to focus on today are: Getting Started File/Folder Purpose app/ Contains the controllers, models, and views for your application. You will do most of your work here. config/ Configure your application's runtime rules, routes, database, and more. db/ Shows your current database schema, as well as the database migrations. public/ The only folder seen to the world as-is. If you put files in here, they will be served directly without any processing by Rails. app/assets/ This is where your images, JavaScript, stylesheets (CSS), and other static files should go. Modern Rails apps use something called the Assets Pipeline, which combines all the JavaScript and CSS files in this directory into a single file for speediness.
to create everything necessary to get up and running with topics. topic tells Rails the name of the new model. title:string says that topics have a title, which is a "string". description:text says that topics have a description which is a "text". (What's the difference between "string" and "text"? Basically "text" is for strings that might be very long.) Scaffolding a Model
to run small Ruby programs (tasks) that you use often in your application. Here, rake db:migrate is a task provided by the Rails framework. It uses the migration file we just created (db/migrate/201xxxxxxxxxxx_create_topics.rb) to change the database. Scaffolding a Model
same. They need to store records and provide a way to do the following: • Create new records in the database • Read or show the records in the database • Update existing records • Destroy or delete records Because these 4 actions (CRUD) are so common, Rails includes the scaffold command to make creating them easier. CRUD With Scaffolding
The Rails scaffold did it for you. Let's take a closer look at some of the files Rails created: app/models/topic.rb This file contains code for our topic model. If you look at it, it's nearly blank. Creating, reading, updating, and deleting records are built into Rails. CRUD With Scaffolding
"Edit". app/views/topics/_form.html.erb You may have noticed that the page for new topics and the page to edit topics looked similar. That's because they both use the code from this file to show a form. This file is called a partial since it only contains code for part of a page. Partials always have filenames starting with an underscore character. CRUD With Scaffolding
part of the scaffold If you look, you'll see a method (a line beginning with def) for each of the views listed above (except _form.html.erb) CRUD With Scaffolding
default address for your site is topics#index. topics#index is the topics list page (the topics controller with the index action). • Rails routes control how URLs (web addresses) get matched with code on the server. Similar to how addresses match with houses and apartments. • The file config/routes.rb is like an address directory listing the possible addresses and which code goes with each one • routes.rb uses some shortcuts so it doesn't always show all the possible URLs. To explore the URLs in more detail we can use the terminal. Setting the Default Page
should get something that looks like this: $ rake routes Prefix Verb URI Pattern Controller#Action topics GET /topics(.:format) topics#index POST /topics(.:format) topics#create new_topic GET /topics/new(.:format) topics#new edit_topic GET /topics/:id/edit(.:format) topics#edit topic GET /topics/:id(.:format) topics#show PATCH /topics/:id(.:format) topics#update PUT /topics/:id(.:format) topics#update DELETE /topics/:id(.:format) topics#destroy root GET / topics#index This shows all the URLs your application responds to. The code that starts with colons are variables so :id means the id number of the record. The code in parenthesis is optional. Setting the Default Page
development by going to http://localhost:3000/rails/info. You'll see something like this: You'll also see that table in Rails whenever you try to access an invalid route (try http://localhost:3000/sandwich) Setting the Default Page
the paths that are available in your app by using rails console in the terminal: $ rails console >> app.topics_path => "/topics" >> app.topics_url => "http://www.example.com/topics" app is a special object that represents your entire application. You can ask it about its routes, play with its database connections, or make pseudo-web requests against it with get or post (and lots more). Setting the Default Page
"vote" • The only thing really different is the integer we added called topic_id. • topic_id is the data we need to draw the line between votes and topics. • We didn't generate a full scaffold this time because we aren't going to do the full CRUD for votes; they're just going to be considered part of topics as-is. Voting On Topics
@topic = Topic.find(params[:id]) @topic.votes.create redirect_to(topics_path) end @topic = Topic.find(params[:id]) finds the topic in the database with that id and stores it in the variable @topic. @topic.votes.create creates a new vote for the current topic and saves it in the database. redirect_to(topics_path) tells the browser to go back to topics_path (the topics list). Allow People To Vote
has, plus the word "vote" or "votes" accordingly. • button_to '+1' creates an HTML button with the text "+1". • upvote_topic_path(topic) creates the appropriate URL for the action we want to invoke. In this case, we want to upvote the current topic. • upvote_topic_path(topic) would return /topics/42/upvote (if topic.id was 42) • method: :post ensures we do the create action of CRUD, not the read action. Allow People To Vote