Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

11.
Lab 1b: Scaffolding1. In your Lab Environment, go to the Console tab.2. Press ctrl-c and [enter] to make sure you have a prompt3. Create your application by typing (all on one line): rails generate scaffold course title:string description:text4. Add the database from your scaffold by typing: rake db:migrate5. Run the server again: rails server6. Browse to http://(your environment address):3000/courses.7. Pretend you are a user and add, edit, and delete courses

12.
Lab 1c: More Scaffolding1. In your Lab Environment, go to the Console tab.2. Press ctrl-c and [enter] to make sure you have a prompt3. Now that we have courses, we’re going to add assignments to our application. Do this by typing (all on one line): rails generate scaffold assignment description:text assigned_on:date due_on:date4. Update the database by by typing: rake db:migrate5. Run the server again: rails server6. Browse to http://(your environment address):3000/assignments.7. Pretend you are a user and add, edit, and delete assignments

15.
Unit 22.1 What is MVCLab 2a Working with Views2.2 RoutesLab 2b Changing the Home Directory2.3 Questions

16.
2.1 What is MVC?• The smart way to build applications• Separates Business Logic, HTML, and Actions • Model: talks to database • View: displays information • Controller: responds when user does something

17.
2.1 What is MVC? Hey Model, can I have some data? CONTROLLER Hey View, can you show VIEW the data? MODEL Sure! Sure, heres some data...

18.
Lab 2a: Working with Views1. Browse to http://(your environment address):3000/courses2. Make sure you have at least one course by clicking on show3. Click the Editor tab in your Lab Environment4. Open little_school_house/app/views/courses/show.html.erb5. Turn the title into a header by putting h1 tags around it (delete lines in red; type lines in blue): <p> <b>Title:</b> <%= @course.title %> </p> <h1><%= @course.title %></h1>6. Also, remove the “Description” label: <b>Description:</b>7. Run the server again: rails server8. View the changes.

20.
2.2 Routes• Looks at which URL a user clicked on• Decides which Controller to use based on the URL• Can set root (home) page root :to =>"courses#index"

21.
Lab 2c: Changing the Home Directory1. In the Console tab, run the server if it isn’t running already: rails server2. Browse to http://(your environment address):3000/. Notice that you get the default page.3. In the Editor tab, delete the ﬁle public/index.html.4. Reload the page the in the browser and notice that you get an error.5. In the Editor tab, edit the ﬁle conﬁg/routes.rb. Add the line in blue below: resources :courses root :to => courses#index6. In the browser, reload the page. Notice that your page now shows up.

22.
Lab 2d: Redirection1. In the Editor tab, edit conﬁg/routes.rb again to use a redirect (changed text in blue): root :to => redirect("/courses")2. In the browser, reload the page. Notice that the front page now redirects to /courses.

39.
Lab 4-2b: Modifying the course show view1. In the Editor tab, edit app/views/courses/show.html.erb. Replace the static list that we added earlier with this code (changes in blue). We will also add a link to create a new assignment: <h2>Assignments</h2> <ul> <% @course.assignments.each do |assignment| %> <li> <%= "#{assignment.due_on}: #{assignment.description}" %> </li> <% end %> </ul> <%= link_to "Create a new assignment", new_course_assignment_path(@course) %>

40.
Lab 4-2c: Modifying the new assignment1. In the Editor tab, edit app/views/assignment/new.html.erb. Since the routes have changed, we need to change the link for ‘Back’. Make the change in blue: <%= link_to Back, course_path(@course) %>2. We will also need to change the form itself, which is in a partial. Edit it by opening up app/views/assignment/_form.html.erb. Edit the form so that it reﬂects that an assignment belongs to a course. Make the changes in blue (make certain to include the square brackets ([])): <%= form_for([@course, @assignment]) do |f| %>