Model to Controller to View

Premium Content - Free Preview

The last page demonstrated how the controller can pass information to the view. Usually, the controller gets this information from the model, so let's go ahead and code this connection. (You should already have created the model in Rails Model & Data.)

Making Stuff Available from the Controller

It would be good if we could show a product on the home page. We already created some products in the console, so let's go ahead and display one of them. Our first task will be to add a line to our controller so a product is available in the view.

Model Methods showed a method called newest that gets the newest product from the database. Can you call that method and assign what it returns to a variable called @product? Add this line of code to the home action of your product controller.

The Code

Displaying Stuff in The View

Now that the controller made the newest product available, let's make sure we can access it from the view.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]