Rails for .NET Developers

Rails for .NET Developers

Prepared exclusively for Trieu Nguyen What readers are saying about Rails for .NET Developers Eng and Cohen, like a modern-day Lewis and Clark, have blazed a trail that .NET developers can follow to the new frontier. If you are a .NET developer and are considering moving to Ruby on Rails, then this book is the place to start. James Avery President and CEO, Infozerk, Inc. If you’re ready to make the rewarding trip from .NET to Rails, this book will give you the road map you need. Mike Gunderloy Former .NET Developer, http://afreshcup.com This book will be a tremendous aid to anyone making the transition from .NET to Ruby on Rails. All the major topics a new Rails developer should become familiar with are covered in great detail. Michael Leung Lead Developer, Urbis.com Jeff and Brian have done a wonderful job of explaining Ruby on Rails to .NET developers in this book...but there’s more value here than just learning a hot technology. Jeff and Brian show you how Ruby on Rails can make you a better developer no matter what platform you use, as well as how it can influence how you design and write web applica- tions. Brian Hogan Principal Consultant, New Auburn Personal Computer Services LLC Download at Boykma.Com Prepared exclusively for Trieu Nguyen Rails for .NET Developers Jeff Cohen Brian Eng The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Download at Boykma.Com Prepared exclusively for Trieu Nguyen Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Jeff Cohen and Brian Eng. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-20-4 ISBN-13: 978-1-934356-20-3 Printed on acid-free paper. P2.0 printing, December 2008 Version: 2009-4-20 Download at Boykma.Com Prepared exclusively for Trieu Nguyen Contents Acknowledgments 8 Preface 9 What’s in This Book ........................ 9 Who This Book Is For ........................ 10 About the Environment and Version Requirements . 10 Conventions ............................. 11 Online Resources .......................... 11 I Hello, Rails 12 1 Getting Started with Rails 13 1.1 WhyRails? .......................... 13 1.2 Culture Shock and Its Treatment ............. 15 1.3 Let’s Get This Party Started ................ 17 1.4 Installing Ruby and Rails ................. 18 1.5 Connecting to a Database ................. 21 1.6 Instant Gratification—Your First Rails App . 22 2 Switching to Ruby 31 2.1 Ruby vs. .NET for the Impatient .............. 32 2.2 Our First Ruby Program .................. 34 2.3 Working with String Objects ................ 35 2.4 irb Is Your New “Immediate Mode” ............ 38 2.5 Arrays ............................. 38 2.6 Symbols ........................... 44 2.7 Hashes ............................ 46 2.8 Everything Is an Object ................... 48 2.9 Classes and Objects ..................... 50 2.10 Loops ............................. 56 Download at Boykma.Com Prepared exclusively for Trieu Nguyen CONTENTS 6 3 Ruby Skills for the Rails Developer 58 3.1 Working with Collections and Iterators .......... 59 3.2 Reusing Code with Base Classes ............. 66 3.3 Where’d My Interfaces Go? ................. 68 3.4 Code Reuse Using Modules ................ 70 3.5 Ruby Wrap-Up ........................ 73 II Rails in Action 74 4 A Bird’s Eye View of Rails 75 4.1 Comparing Web Architectures ............... 76 4.2 Environments in Rails ................... 81 4.3 Configuring Data Access .................. 83 4.4 Receiving HTTP Requests ................. 84 4.5 Generating HTTP Responses ................ 85 5 Rails Conventions 88 5.1 MVC: Separating Responsibilities in Your Application . 88 5.2 Putting It to REST ...................... 94 6 CRUD with ActiveRecord 100 6.1 Displaying a Grid of Data in a Table ........... 100 6.2 Sorting, Filtering, and Paging Data ............ 108 6.3 Validating User Input .................... 119 6.4 Representing Relationships Between Tables . 124 7 Directing Traffic with ActionController 128 7.1 Routing and Pretty URLs .................. 128 7.2 User Authentication ..................... 133 7.3 Providing an API ....................... 142 8 Exploring Forms, Layouts, and Partials 150 8.1 Diving Into Forms ...................... 150 8.2 Using Layouts Instead of Master Pages ......... 161 8.3 Creating Partials Instead of User Controls ........ 166 Download at Boykma.Com Report erratum Prepared exclusively for Trieu Nguyen this copy is (P2.0 printing, December 2008) CONTENTS 7 9 Creating Rich User Experiences with Ajax 172 9.1 First, a Little Background ................. 172 9.2 Partial-Page Updates .................... 174 9.3 Visual Effects on the Web ................. 184 III Advanced Topics 190 10 Test-Driven Development on Rails 191 10.1 A First Look at Test/Unit .................. 192 10.2 Test-Driven Development with Test/Unit ........ 195 10.3 DRYing Up Tests with Setup Methods .......... 204 10.4 Providing Test Data with Fixtures ............. 206 10.5 Behavior-Driven Development with Shoulda . 210 11 Integrating with .NET 216 11.1 Using a Rails Web Service from .NET ........... 216 11.2 Using a SOAP Web Service from Ruby .......... 227 12 Finishing Touches 232 12.1 Getting to Know RubyGems ................ 232 12.2 Using Gems in Your Rails Applications .......... 238 12.3 Learning More About rake ................. 240 12.4 Distributing Rails with Your Application ......... 245 12.5 Deployment Considerations ................ 248 13 Inspired by Rails 252 13.1 IronRuby ........................... 253 13.2 ASP.NET MVC ........................ 257 13.3 Other Open Source Projects ................ 261 13.4 How About You? ....................... 262 Bibliography 264 Index 265 Download at Boykma.Com Report erratum Prepared exclusively for Trieu Nguyen this copy is (P2.0 printing, December 2008) Acknowledgments We are very grateful to those who helped make this book a reality. Susannah Pfalzer’s time, patience, and insight were enormously valu- able to us time and again during the writing of this book. We also had great help from our reviewers—Geoffrey Grosenbach, James Avery, Michael Dwan, Dianne Siebold, Mike Gunderloy, Michael Leung, Scott Hanselman, and Ron Green—and our publishers, Dave Thomas and Andy Hunt. They not only helped us with technical details, but their thoughtfulness and feedback elevated our writing to the next level. We would also like to thank Michael Manley, Scott Epskamp, and the rest of Jeff’s colleagues at Leapfrog Online, whose understanding and flexi- bility helped make this book possible. Jeff says: I could not have written this book without the encouragement and support of my beautiful wife, Susannah, and our two wonderful children, Laura and Emily. I also thank my parents, Bill and Marilyn Cohen, who first taught me how to read and write—two skills that came in quite handy. Brian says: I’d like to thank my wonderful wife, Erika, for her support and inspiration—but especially for caring for our two baby girls, Katie and Abbie, all day and night while I’ve been working on this book! Also thanks to my mom and dad, Alfred and Patricia Eng, for the Apple II that got all this computer stuff started. Download at Boykma.Com Prepared exclusively for Trieu Nguyen Preface As a .NET developer, you’ve probably heard the buzz surrounding Ruby on Rails. It’s true: Rails enables you to create database-driven web applications with remarkable speed and ease. And like many other open source projects, Rails has been most easily adopted by individuals and organizations already immersed in the open source community. That means, as a Microsoft developer, you face unique challenges learning not just Rails but all the open source technologies that go along with it. This book will be your guide as you navigate this new terrain. For a .NET developer, learning Rails is as much about the cultural and philosophical shifts in thinking as it is about the technical learning curve. In this book, we hope to break down some of these barriers for you. We have learned a lot of valuable lessons from Rails that we’ve applied to our .NET development too; if you take anything away from this book, it will be a new way of thinking about software development— the Rails way. What’s in This Book To get things going, we’ll introduce the Rails development environment and the core set of tools you’ll need to be an effective Rails developer from the very beginning. Nothing is better to get your feet wet than to actually write some code and build a small application, so that’s exactly what we’ll do. Becoming a skilled Rails developer is all about learning Ruby. So, we’ll take an in-depth look at the Ruby language and how to understand it from a Microsoft developer’s perspective. We’ll ease you into it and gradually move into more advanced topics. Once we’ve established a solid base of Ruby knowledge, we’ll dive head- first into the Rails framework.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    276 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us