Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 297 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
297
Dung lượng
1,99 MB
Nội dung
[...]... recipe If you want to find recipes that have something to do with Mail, for example, find the Mail 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 this page for each appropriate recipe Ajax API Tips Automation Configuration Database Development Process Extending Rails HTML Integration Rails Internals Mail Plugins Rails 1.1+ Search Security... Problem One of the things I love about Rails is that, though I enjoy taking advantage of many of the user interface benefits that JavaScript provides, it saves me from writing JavaScript code that I really don’t like to write Rails is full of magical one-liners that create exciting user interface effects—all without having to touch a line of JavaScript Sadly, Rails doesn’t solve every user interface... Search Security Style Testing Troubleshooting XML Part I User Interface Recipes 1 Recipe 1 In-Place Form Editing Problem Your application has one or more pieces of data that are often edited by your users—usually very quickly You want to give your users an easy way to edit application data in place without opening a separate form Solution Rails makes in-place editing easy with the script.aculo.us InPlaceEditor... fortunate side effect of the way the selectOptionsHTML parameter is implemented is that it’s easy to use with the Rails form options helpers Putting all our work together, here’s app/views/contacts/show.rhtml modified to use our new helper Notice that we are supplying the country list via the built-in Rails country_options_for_select( ) helper Download MakingYourOwnJavaScriptHelper/app/views/contacts/show.rhtml... entry that’s going to be displayed to the world Solution We can easily accomplish a live preview effect using the built-in Rails JavaScript helpers For this recipe, we’ll create a live preview of an extremely simple form for creating a diary entry The first step in creating any “Ajaxy” Rails effect is to make sure you’re including the right JavaScript libraries For the live preview effect, we need to include... We’ll assume that we’ve already created the necessary database tables and model classes and that the Active Record migration to create this table looks like the following: Download 3_add _recipes. rb def self.up create_table "recipes" do |t| t.column "name" , :string t.column "region" , :string t.column "instructions" , :text end create_table "ingredients" do |t| t.column "recipe_id" , :integer t.column "name"... INCLUDE THE JAVASCRIPT FILES! Now that the magic spells are included, we can invoke them: This causes Rails to create a text box for you with all the required JavaScript attached to it As with most Rails helpers, the method text_field_with_auto_complete( ) isn’t doing anything you couldn’t do manually But, if you’ve ever had to attach JavaScript events... accept the additional selectOptionsHTML key Now we have the JavaScript side working, how can we remove the need for JavaScript programming altogether? It’s time to make a helper! As we saw earlier, the Rails JavaScript helpers essentially just generate text that happens to be JavaScript What do we need to generate for this helper? Basically, we just need to generate the equivalent code that we wrote... received a 404 (not found) response code in return Now we could go into our ContactsController and define the method set_contact_name( ), but since we’re doing something so conventional, we can rely on a Rails convention to do the work for us! Open the controller app/controllers/contacts_controller.rb, and add the following line right after the beginning of the class definition (line 2 would be a good place):... source code? What I did was to create an action with the name that I saw in the logs that looked like the following: def set_contact_name raise params.inspect end When I submitted the form, I saw the Rails error message with a list of the submitted parameters at the top set_contact_postal_code( ) The in-place edit control form will pass two notable parameters: the contact’s id, aptly named id and the . also set up a forum for Rails Recipes readers to discuss t he recipes, help each other with problems, expand on the solutions, and even write new recipes. While Rails Recipes was in beta, th. noted, should work with Rails 1.0 or higher. Several recipes cover new feat ures that were released with Rails 1.1. Resources The best place to go for Rails infor mation is the Rails website. 2 From there,. the beginning stages of learning Rails, be sure to have a copy of Agile Web Development with Rails [ TH05] and a bookmark to the Rails API documentation handy. 1 Rails Version The examples in this