Cleaned up wording and tags.
Replaced <br /> with <br> for consistency; Added note about singular/plural distinction (which was previously confusing to a Rails noob); Rephrased explanation of how form is routed to controller, again to reduce confusion; and added explanation to replace incorrect link. [ci skip]
This commit is contained in:
parent
c6009b2d0d
commit
726530689e
|
@ -941,9 +941,9 @@ Creates a form and a scope around a specific model object that is used as a base
|
|||
```html+erb
|
||||
<%= form_for @post do |f| %>
|
||||
<%= f.label :title, 'Title' %>:
|
||||
<%= f.text_field :title %><br />
|
||||
<%= f.text_field :title %><br>
|
||||
<%= f.label :body, 'Body' %>:
|
||||
<%= f.text_area :body %><br />
|
||||
<%= f.text_area :body %><br>
|
||||
<% end %>
|
||||
```
|
||||
|
||||
|
|
|
@ -69,12 +69,12 @@ Rails' old strategy was to append a date-based query string to every asset linke
|
|||
|
||||
The query string strategy has several disadvantages:
|
||||
|
||||
1. **Not all caches will reliably cache content where the filename only differs by query parameters**<br />
|
||||
1. **Not all caches will reliably cache content where the filename only differs by query parameters**<br>
|
||||
[Steve Souders recommends](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/), "...avoiding a querystring for cacheable resources". He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation.
|
||||
|
||||
2. **The file name can change between nodes in multi-server environments.**<br />
|
||||
2. **The file name can change between nodes in multi-server environments.**<br>
|
||||
The default query string in Rails 2.x is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.
|
||||
3. **Too much cache invalidation**<br />
|
||||
3. **Too much cache invalidation**<br>
|
||||
When static assets are deployed with each new release of code, the mtime(time of last modification) of _all_ these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.
|
||||
|
||||
Fingerprinting fixes these problems by avoiding query strings, and by ensuring that filenames are consistent based on their content.
|
||||
|
|
|
@ -346,7 +346,7 @@ Next, the partial that this line will render needs to exist. Create a new direct
|
|||
<h3>New comment</h3>
|
||||
<%= form_for [@post, @post.comments.build] do |f| %>
|
||||
<p>
|
||||
<%= f.label :text %><br />
|
||||
<%= f.label :text %><br>
|
||||
<%= f.text_area :text %>
|
||||
</p>
|
||||
<%= f.submit %>
|
||||
|
@ -515,7 +515,7 @@ First, the `author_name` text field needs to be added to the `app/views/blorgh/p
|
|||
|
||||
```html+erb
|
||||
<div class="field">
|
||||
<%= f.label :author_name %><br />
|
||||
<%= f.label :author_name %><br>
|
||||
<%= f.text_field :author_name %>
|
||||
</div>
|
||||
```
|
||||
|
|
|
@ -138,7 +138,7 @@ application. Most of the work in this tutorial will happen in the `app/` folder,
|
|||
|config/|Configure your application's runtime rules, routes, database, and more. This is covered in more detail in [Configuring Rails Applications](configuring.html)|
|
||||
|config.ru|Rack configuration for Rack based servers used to start the application.|
|
||||
|db/|Contains your current database schema, as well as the database migrations.|
|
||||
|Gemfile<br />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see [the Bundler website](http://gembundler.com) |
|
||||
|Gemfile<br>Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see [the Bundler website](http://gembundler.com) |
|
||||
|lib/|Extended modules for your application.|
|
||||
|log/|Application log files.|
|
||||
|public/|The only folder seen to the world as-is. Contains the static files and compiled assets.|
|
||||
|
@ -264,11 +264,14 @@ Blog::Application.routes.draw do
|
|||
end
|
||||
```
|
||||
|
||||
If you run `rake routes`, you'll see that all the routes for the
|
||||
standard RESTful actions.
|
||||
If you run `rake routes`, you'll see that it has defined routes for all the
|
||||
standard RESTful actions. The meaning of the prefix column (and other columns)
|
||||
will be seen later, but for now notice that Rails has inferred the
|
||||
singular form `post` and makes meaningful use of the distinction.
|
||||
|
||||
```bash
|
||||
$ rake routes
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
posts GET /posts(.:format) posts#index
|
||||
POST /posts(.:format) posts#create
|
||||
new_post GET /posts/new(.:format) posts#new
|
||||
|
@ -394,9 +397,27 @@ Edit the `form_for` line inside `app/views/posts/new.html.erb` to look like this
|
|||
<%= form_for :post, url: posts_path do |f| %>
|
||||
```
|
||||
|
||||
In this example, the `posts_path` helper is passed to the `:url` option. What Rails will do with this is that it will point the form to the `create` action of the current controller, the `PostsController`, and will send a `POST` request to that route.
|
||||
|
||||
By using the `post` method rather than the `get` method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web.
|
||||
In this example, the `posts_path` helper is passed to the `:url` option.
|
||||
To see what Rails will do with this, we look back at the output of
|
||||
`rake routes`:
|
||||
```bash
|
||||
$ rake routes
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
posts GET /posts(.:format) posts#index
|
||||
POST /posts(.:format) posts#create
|
||||
new_post GET /posts/new(.:format) posts#new
|
||||
edit_post GET /posts/:id/edit(.:format) posts#edit
|
||||
post GET /posts/:id(.:format) posts#show
|
||||
PATCH /posts/:id(.:format) posts#update
|
||||
PUT /posts/:id(.:format) posts#update
|
||||
DELETE /posts/:id(.:format) posts#destroy
|
||||
root / welcome#index
|
||||
```
|
||||
The `posts_path` helper tells Rails to point the form
|
||||
to the URI Pattern associated with the `posts` prefix; and
|
||||
the form will (by default) send a `POST` request
|
||||
to that route. This is associated with the
|
||||
`create` action of the current controller, the `PostsController`.
|
||||
|
||||
With the form and its associated route defined, you will be able to fill in the form and then click the submit button to begin the process of creating a new post, so go ahead and do that. When you submit the form, you should see a familiar error:
|
||||
|
||||
|
@ -1016,9 +1037,14 @@ content:
|
|||
```
|
||||
|
||||
Everything except for the `form_for` declaration remained the same.
|
||||
How `form_for` can figure out the right `action` and `method` attributes when building the form
|
||||
will be explained in [just a moment](/form_helpers.html#binding-a-form-to-an-object).
|
||||
For now, let's update the `app/views/posts/new.html.erb` view to use this new partial, rewriting it
|
||||
The reason we can use this shorter, simpler `form_for` declaration
|
||||
to stand in for either of the other forms is that `@post` is a *resource*
|
||||
corresponding to a full set of RESTful routes, and Rails is able to infer
|
||||
which URI and method to use.
|
||||
For more information about this use of `form_for`, see
|
||||
[Resource-oriented style](//api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style).
|
||||
|
||||
Now, let's update the `app/views/posts/new.html.erb` view to use this new partial, rewriting it
|
||||
completely:
|
||||
|
||||
```html+erb
|
||||
|
@ -1289,11 +1315,11 @@ So first, we'll wire up the Post show template
|
|||
<h2>Add a comment:</h2>
|
||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||
<p>
|
||||
<%= f.label :commenter %><br />
|
||||
<%= f.label :commenter %><br>
|
||||
<%= f.text_field :commenter %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.label :body %><br />
|
||||
<%= f.label :body %><br>
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
<p>
|
||||
|
@ -1369,11 +1395,11 @@ template. This is where we want the comment to show, so let's add that to the
|
|||
<h2>Add a comment:</h2>
|
||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||
<p>
|
||||
<%= f.label :commenter %><br />
|
||||
<%= f.label :commenter %><br>
|
||||
<%= f.text_field :commenter %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.label :body %><br />
|
||||
<%= f.label :body %><br>
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
<p>
|
||||
|
@ -1435,11 +1461,11 @@ following:
|
|||
<h2>Add a comment:</h2>
|
||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||
<p>
|
||||
<%= f.label :commenter %><br />
|
||||
<%= f.label :commenter %><br>
|
||||
<%= f.text_field :commenter %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.label :body %><br />
|
||||
<%= f.label :body %><br>
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
<p>
|
||||
|
@ -1465,11 +1491,11 @@ create a file `app/views/comments/_form.html.erb` containing:
|
|||
```html+erb
|
||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||
<p>
|
||||
<%= f.label :commenter %><br />
|
||||
<%= f.label :commenter %><br>
|
||||
<%= f.text_field :commenter %>
|
||||
</p>
|
||||
<p>
|
||||
<%= f.label :body %><br />
|
||||
<%= f.label :body %><br>
|
||||
<%= f.text_area :body %>
|
||||
</p>
|
||||
<p>
|
||||
|
|
|
@ -88,7 +88,7 @@ If we want to display the properties of all the books in our view, we can do so
|
|||
<% end %>
|
||||
</table>
|
||||
|
||||
<br />
|
||||
<br>
|
||||
|
||||
<%= link_to "New book", new_book_path %>
|
||||
```
|
||||
|
@ -1026,7 +1026,7 @@ You can also pass local variables into partials, making them even more powerful
|
|||
```html+erb
|
||||
<%= form_for(zone) do |f| %>
|
||||
<p>
|
||||
<b>Zone name</b><br />
|
||||
<b>Zone name</b><br>
|
||||
<%= f.text_field :name %>
|
||||
</p>
|
||||
<p>
|
||||
|
|
|
@ -326,7 +326,7 @@ The following shows how to replace use `Rack::Builder` instead of the Rails supp
|
|||
config.middleware.clear
|
||||
```
|
||||
|
||||
<br />
|
||||
<br>
|
||||
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
||||
|
||||
```ruby
|
||||
|
|
Loading…
Reference in New Issue