Permalink
Browse files

Fix remaining formatting problems in the guide

  • Loading branch information...
1 parent 5a4622b commit 721afdcc4b58c65f36122b10ec998b913a147912 @sikachu sikachu committed with sikachu Sep 4, 2012
@@ -120,15 +120,15 @@ If you're writing a web service application, you might find yourself more comfor
So for example, if you are sending this JSON parameter:
-```
+```json
{ "company": { "name": "acme", "address": "123 Carrot Street" } }
```
You'll get `params[:company]` as `{ :name => "acme", "address" => "123 Carrot Street" }`.
Also, if you've turned on `config.wrap_parameters` in your initializer or calling `wrap_parameters` in your controller, you can safely omit the root element in the JSON/XML parameter. The parameters will be cloned and wrapped in the key according to your controller's name by default. So the above parameter can be written as:
-```
+```json
{ "name": "acme", "address": "123 Carrot Street" }
```
@@ -309,7 +309,7 @@ redirect_to root_url, :flash => { :referral_code => 1234 }
The `destroy` action redirects to the application's `root_url`, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display any error alerts or notices from the flash in the application's layout:
-```ruby
+```erb
<html>
<!-- <head/> -->
<body>
@@ -328,7 +328,7 @@ This way, if an action sets a notice or an alert message, the layout will displa
You can pass anything that the session can store; you're not limited to notices and alerts:
-```ruby
+```erb
<% if flash[:just_signed_up] %>
<p class="welcome">Welcome to our site!</p>
<% end %>
@@ -549,7 +549,7 @@ The way this is done is to add a non-guessable token which is only known to your
If you generate a form like this:
-```ruby
+```erb
<%= form_for @user do |f| %>
<%= f.text_field :username %>
<%= f.text_field :password %>
@@ -67,7 +67,7 @@ Just like controllers, any instance variables we define in the method become ava
Create a file called `welcome_email.html.erb` in `app/views/user_mailer/`. This will be the template used for the email, formatted in HTML:
-```erb
+```html+erb
<!DOCTYPE html>
<html>
<head>
@@ -171,21 +171,21 @@ Defining custom headers are simple, you can do it one of three ways:
* Defining a header field as a parameter to the `mail` method:
-```ruby
-mail("X-Spam" => value)
-```
+ ```ruby
+ mail("X-Spam" => value)
+ ```
* Passing in a key value assignment to the `headers` method:
-```ruby
-headers["X-Spam"] = value
-```
+ ```ruby
+ headers["X-Spam"] = value
+ ```
* Passing a hash of key value pairs to the `headers` method:
-```ruby
-headers {"X-Spam" => value, "X-Special" => another_value}
-```
+ ```ruby
+ headers {"X-Spam" => value, "X-Special" => another_value}
+ ```
TIP: All `X-Value` headers per the RFC2822 can appear more than once. If you want to delete an `X-Value` header, you need to assign it a value of `nil`.
@@ -195,20 +195,20 @@ Adding attachments has been simplified in Action Mailer 3.0.
* Pass the file name and content and Action Mailer and the Mail gem will automatically guess the mime_type, set the encoding and create the attachment.
-```ruby
-attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
-```
+ ```ruby
+ attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
+ ```
NOTE: Mail will automatically Base64 encode an attachment. If you want something different, pre-encode your content and pass in the encoded content and encoding in a `Hash` to the `attachments` method.
* Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.
-```ruby
-encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
-attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
- :encoding => 'SpecialEncoding',
- :content => encoded_content }
-```
+ ```ruby
+ encoded_content = SpecialEncode(File.read('/path/to/filename.jpg'))
+ attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
+ :encoding => 'SpecialEncoding',
+ :content => encoded_content }
+ ```
NOTE: If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.
@@ -218,28 +218,28 @@ Action Mailer 3.0 makes inline attachments, which involved a lot of hacking in p
* Firstly, to tell Mail to turn an attachment into an inline attachment, you just call `#inline` on the attachments method within your Mailer:
-```ruby
-def welcome
- attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
-end
-```
+ ```ruby
+ def welcome
+ attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
+ end
+ ```
* Then in your view, you can just reference `attachments[]` as a hash and specify which attachment you want to show, calling `url` on it and then passing the result into the `image_tag` method:
-```erb
-<p>Hello there, this is our image</p>
+ ```html+erb
+ <p>Hello there, this is our image</p>
-<%= image_tag attachments['image.jpg'].url %>
-```
+ <%= image_tag attachments['image.jpg'].url %>
+ ```
* As this is a standard call to `image_tag` you can pass in an options hash after the attachment URL as you could for any other image:
-```erb
-<p>Hello there, this is our image</p>
+ ```html+erb
+ <p>Hello there, this is our image</p>
-<%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
- :class => 'photos' %>
-```
+ <%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
+ :class => 'photos' %>
+ ```
#### Sending Email To Multiple Recipients
@@ -262,7 +262,7 @@ The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:
#### Sending Email With Name
Sometimes you wish to show the name of the person instead of just their email address when they receive the email. The trick to doing that is
-to format the email address in the format `"Name &lt;email&gt;"`.
+to format the email address in the format `"Name <email>"`.
```ruby
def welcome_email(user)
@@ -470,6 +470,8 @@ Action Mailer Configuration
The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
+| Configuration | Description |
+|---------------|-------------|
|`template_root`|Determines the base from which template references will be made.|
|`logger`|Generates information on the mailing run if available. Can be set to `nil` for no logging. Compatible with both Ruby's own `Logger` and `Log4r` loggers.|
|`smtp_settings`|Allows detailed configuration for `:smtp` delivery method:<ul><li>`:address` - Allows you to use a remote mail server. Just change it from its default "localhost" setting.</li><li>`:port` - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>`:domain` - If you need to specify a HELO domain, you can do it here.</li><li>`:user_name` - If your mail server requires authentication, set the username in this setting.</li><li>`:password` - If your mail server requires authentication, set the password in this setting.</li><li>`:authentication` - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of `:plain`, `:login`, `:cram_md5`.</li><li>`:enable_starttls_auto` - Set this to `false` if there is a problem with your server certificate that you cannot resolve.</li></ul>|
@@ -133,7 +133,7 @@ Within an ERB template Ruby code can be included using both `<% %>` and `<%= %>`
Consider the following loop for names:
-```erb
+```html+erb
<b>Names of all the people</b>
<% @people.each do |person| %>
Name: <%= person.name %><br/>
@@ -142,7 +142,7 @@ Consider the following loop for names:
The loop is setup in regular embedding tags `<% %>` and the name is written using the output embedding tag `<%= %>`. Note that this is not just a usage suggestion, for Regular output functions like print or puts won't work with ERB templates. So this would be wrong:
-```erb
+```html+erb
<%# WRONG %>
Hi, Mr. <% puts "Frodo" %>
```
@@ -226,13 +226,13 @@ Partial templates – usually just called "partials" – are another device for
To render a partial as part of a view, you use the `render` method within the view:
-```ruby
+```erb
<%= render "menu" %>
```
This will render a file named `_menu.html.erb` at that point within the view is being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
-```ruby
+```erb
<%= render "shared/menu" %>
```
@@ -242,7 +242,7 @@ That code will pull in the partial from `app/views/shared/_menu.html.erb`.
One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
-```erb
+```html+erb
<%= render "shared/ad_banner" %>
<h1>Products</h1>
@@ -346,15 +346,15 @@ In the `show` template, we'll render the `post` partial wrapped in the `box` lay
*posts/show.html.erb*
-```ruby
+```erb
<%= render :partial => 'post', :layout => 'box', :locals => {:post => @post} %>
```
The `box` layout simply wraps the `post` partial in a `div`:
*posts/_box.html.erb*
-```ruby
+```html+erb
<div class='box'>
<%= yield %>
</div>
@@ -364,7 +364,7 @@ The `post` partial wraps the post's `body` in a `div` with the `id` of the post
*posts/_post.html.erb*
-```ruby
+```html+erb
<%= div_for(post) do %>
<p><%= post.body %></p>
<% end %>
@@ -386,7 +386,7 @@ You can also render a block of code within a partial layout instead of calling `
*posts/show.html.erb*
-```ruby
+```html+erb
<% render(:layout => 'box', :locals => {:post => @post}) do %>
<%= div_for(post) do %>
<p><%= post.body %></p>
@@ -471,7 +471,7 @@ Renders a container tag that relates to your Active Record Object.
For example, given `@post` is the object of `Post` class, you can do:
-```ruby
+```html+erb
<%= content_tag_for(:tr, @post) do %>
<td><%= @post.title %></td>
<% end %>
@@ -487,7 +487,7 @@ This will generate this HTML output:
You can also supply HTML attributes as an additional option hash. For example:
-```ruby
+```html+erb
<%= content_tag_for(:tr, @post, :class => "frontpage") do %>
<td><%= @post.title %></td>
<% end %>
@@ -503,7 +503,7 @@ Will generate this HTML output:
You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given `@posts` is an array of two `Post` objects:
-```ruby
+```html+erb
<%= content_tag_for(:tr, @posts) do |post| %>
<td><%= post.title %></td>
<% end %>
@@ -524,7 +524,7 @@ Will generate this HTML output:
This is actually a convenient method which calls `content_tag_for` internally with `:div` as the tag name. You can pass either an Active Record object or a collection of objects. For example:
-```ruby
+```html+erb
<%= div_for(@post, :class => "frontpage") do %>
<td><%= @post.title %></td>
<% end %>
@@ -745,7 +745,7 @@ end
Allows you to measure the execution time of a block in a template and records the result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.
-```ruby
+```html+erb
<% benchmark "Process data files" do %>
<%= expensive_files_operation %>
<% end %>
@@ -759,7 +759,7 @@ This would add something like "Process data files (0.34523)" to the log, which y
A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static HTML fragments, and so on. This method takes a block that contains the content you wish to cache. See `ActionController::Caching::Fragments` for more information.
-```ruby
+```erb
<% cache do %>
<%= render "shared/footer" %>
<% end %>
@@ -771,15 +771,15 @@ A method for caching fragments of a view rather than an entire action or page. T
The `capture` method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
-```ruby
+```html+erb
<% @greeting = capture do %>
<p>Welcome! The date and time is <%= Time.now %></p>
<% end %>
```
The captured variable can then be used anywhere else.
-```ruby
+```html+erb
<html>
<head>
<title>Welcome!</title>
@@ -798,7 +798,7 @@ For example, let's say we have a standard application layout, but also a special
*app/views/layouts/application.html.erb*
-```ruby
+```html+erb
<html>
<head>
<title>Welcome!</title>
@@ -812,7 +812,7 @@ For example, let's say we have a standard application layout, but also a special
*app/views/posts/special.html.erb*
-```ruby
+```html+erb
<p>This is a special page.</p>
<% content_for :special_script do %>
@@ -985,7 +985,7 @@ There are two types of form helpers: those that specifically work with model att
The core method of this helper, form_for, gives you the ability to create a form for a model instance; for example, let's say that you have a model Person and want to create a new instance of it:
-```ruby
+```html+erb
# Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
<%= form_for @person, :url => { :action => "create" } do |f| %>
<%= f.text_field :first_name %>
@@ -1027,7 +1027,7 @@ check_box("post", "validated")
Creates a scope around a specific model object like form_for, but doesn't create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form:
-```ruby
+```html+erb
<%= form_for @person, :url => { :action => "update" } do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
@@ -1051,7 +1051,7 @@ file_field(:user, :avatar)
Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
-```ruby
+```html+erb
<%= form_for @post do |f| %>
<%= f.label :title, 'Title' %>:
<%= f.text_field :title %><br />
@@ -1360,7 +1360,7 @@ check_box_tag 'accept'
Creates a field set for grouping HTML form elements.
-```ruby
+```html+erb
<%= field_set_tag do %>
<p><%= text_field_tag 'name' %></p>
<% end %>
@@ -1373,7 +1373,7 @@ Creates a file upload field.
Prior to Rails 3.1, if you are using file uploads, then you will need to set the multipart option for the form tag. Rails 3.1+ does this automatically.
-```ruby
+```html+erb
<%= form_tag { :action => "post" }, { :multipart => true } do %>
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
<%= submit_tag %>
@@ -1391,7 +1391,7 @@ file_field_tag 'attachment'
Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
-```ruby
+```html+erb
<%= form_tag '/posts' do %>
<div><%= submit_tag 'Save' %></div>
<% end %>
Oops, something went wrong.

0 comments on commit 721afdc

Please sign in to comment.