Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 398 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
398
Dung lượng
5,33 MB
Nội dung
[...]... without all the contributions from talented Rails developers in the community Working with them has truly been an honor and privilege that I don’t take lightly Thank you all for taking the time to share your tips and tricks! It has been a lot of fun working on this project with you Thanks to my friend Chad Fowler for setting the bar high with his original RailsRecipes [Fow06] book This book tries to follow... the bar high with his original RailsRecipes [Fow06] book This book tries to follow the same style and format as the original I can only hope it’s as valuable The Rails core team was very helpful in reviewing recipes and even contributed a few recipes of their own Thanks, guys, for patiently answering questions and suggesting ideas I owe a debt of gratitude to all the fine folks who participated in the... respond_to blocks are orthogonal You can use respond_to without resources, and vice versa Although Active Resource ships with Rails and uses some of its support classes, it’s not necessarily Rails- specific You can use it to reach out to any server supporting the RESTful conventions of Rails Also See The map.resources method generates routes only for the seven CRUD actions This begs the question, how do I... hasn’t yet been saved, Rails knows that it needs to be created So, the form_for generates a form tag that looks like this: This form will post to our create action because, according to the RESTful routing conventions, the HTTP verb and URL path map to that action However, if the event is an existing record that has previously been saved, then Rails knows we’re trying... administer them through a consistent URL scheme It’s just one more example of Rails conventions removing guesswork Now let’s move on to the second part of the solution Let’s say we’d like to write a client program to administer events remotely We’ll have our 16 1 C REATE A REST FUL R ESOURCE client program speak XML back and forth with our Rails application (imagine what that must sound like!) To do that, let’s... Database Deployment Design E-mail Integration Performance REST Routing Search Security Testing User Interface Part I REST and Routes Recipes 13 Recipe 1 Create a RESTful Resource Problem You’ve heard all the buzz about creating RESTful this and that There’s little doubt that Rails 2.0 heavily favors the REST architectural style1 and that it will continue to do so You’re feeling a little out of the loop,... update it In this case, the form should post to the update action To do that, the form_for method slaps in a hidden field to simulate a PUT operation This in turn triggers the proper RESTful route when Rails intercepts the request It looks something like this in the generated form: OK, so at the end of all this,... controller, but no view templates 1 C REATE A REST FUL R ESOURCE Before we move on to the second part of the solution, let’s dive a bit deeper to see what just happened In some ways, it’s just like the old Rails scaffolding, but there’s a significant twist You may have noticed that the following line was added to our config/routes.rb file: map.resources :events That line is the key ingredient to our RESTful... Resource makes that really easy First, we write a stand-alone Ruby program: the Active Resource client (I typically put these in a services directory, but they could live anywhere.) It doesn’t need to load Rails per se, but we do need to require the activeresource gem: Download Rest/services/event_client.rb require 'rubygems' require 'activeresource' 17 1 C REATE A REST FUL R ESOURCE Then we create an Event... take the time I hope we get to meet someday soon Mike Clark March 2008 mike@clarkware.com 11 T AGS AND T HUMB T ABS 12 1.8 Tags and Thumb Tabs I’ve tried to assign tags to each recipe If you want to find recipes that have something to do with deployment, for example, find the Deployment tab at the edge of this page Then look down the side of the book: you’ll find a thumb tab that lines up with the tab on . developers in the Rails community think is advanced and important. 1.3 Who’s It For? Advanced Rails Recipes is for people who understand Rails and now want to see how experienced Rails developers. Programmers have set up a forum for Advanced Rails Recipes readers to discuss the recipes, help each other with problems, expand on the solutions, and even write new recipes. You can find the forum. your situation. When the first Rails Recipes [ Fow06] book was written, most of the Rails knowledge was concentrated in a small group of experts. These days, with new Rails applications being launched