rails new hotdog
rails g controller home index
This command just generates a home_controller.rb and home/index.html.erb file. Just like we created without a generator script in the beginning. As it does not create a model (no data model required) we do not need to do a rake db:migrate yet.
get "home/index"
# You can have the root of your site routed with "root"
# root 'welcome#index'
# You can have the root of your site routed with "root"
root 'home#index'
rails s
Go to your browser and open localhost:3000 to check that you see the Home index page you just created. We will leave it like this for the moment.
screenshot_1
Now add the functionality that enables the hotdog vendor to add and list his locations. Go back to your command line:
rails g scaffold Location name address suburb date:date start_time:time finish_time:time
Check the migration file that was just created to make sure everything looks right. db/migrate/2013xxxxx_create_locations.rb
screenshot_2
rake db:migrate
screenshot_3
screenshot_4
Let's show the locations on the home page. Go to app/controllers/home_controller.rb and find the index method. Bring in the location data by adding the following line:
def index
@locations = Location.all
end
Now go to app/views/home/index.html.erb, delete the code that is already there and create a table that lists the added locations.
<h1>HotDog King</h1>
<h2>Locations</h2>
<table>
<tr>
<th>Location</th>
<th>Where</th>
<th>When</th>
</tr>
<% @locations.each do |location| %>
<tr>
<td><%= location.name %></td>
<td><%= location.address %>, <%= location.suburb %></td>
<td><%= location.date.to_date %><br>
<%= location.start_time.strftime("%H:%M") %> - <%= location.finish_time.strftime("%H:%M") %></td>
</tr>
<% end %>
-
Add bootstrap to add some style to your page.
-
Try setting a default_scope in the locations model so that the locations show in chronological order: http://guides.rubyonrails.org/active_record_querying.html#applying-a-default-scope
default_scope order: 'locations.date ASC'
-
Add an image to the home page
-
Put an "Add location" link/button on the home page