mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
6 changed files with 53 additions and 27 deletions
|
@ -941,9 +941,9 @@ Creates a form and a scope around a specific model object that is used as a base
|
||||||
```html+erb
|
```html+erb
|
||||||
<%= form_for @post do |f| %>
|
<%= form_for @post do |f| %>
|
||||||
<%= f.label :title, 'Title' %>:
|
<%= f.label :title, 'Title' %>:
|
||||||
<%= f.text_field :title %><br />
|
<%= f.text_field :title %><br>
|
||||||
<%= f.label :body, 'Body' %>:
|
<%= f.label :body, 'Body' %>:
|
||||||
<%= f.text_area :body %><br />
|
<%= f.text_area :body %><br>
|
||||||
<% end %>
|
<% 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:
|
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.
|
[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.
|
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.
|
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.
|
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>
|
<h3>New comment</h3>
|
||||||
<%= form_for [@post, @post.comments.build] do |f| %>
|
<%= form_for [@post, @post.comments.build] do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :text %><br />
|
<%= f.label :text %><br>
|
||||||
<%= f.text_area :text %>
|
<%= f.text_area :text %>
|
||||||
</p>
|
</p>
|
||||||
<%= f.submit %>
|
<%= f.submit %>
|
||||||
|
@ -515,7 +515,7 @@ First, the `author_name` text field needs to be added to the `app/views/blorgh/p
|
||||||
|
|
||||||
```html+erb
|
```html+erb
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<%= f.label :author_name %><br />
|
<%= f.label :author_name %><br>
|
||||||
<%= f.text_field :author_name %>
|
<%= f.text_field :author_name %>
|
||||||
</div>
|
</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/|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.|
|
|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.|
|
|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.|
|
|lib/|Extended modules for your application.|
|
||||||
|log/|Application log files.|
|
|log/|Application log files.|
|
||||||
|public/|The only folder seen to the world as-is. Contains the static files and compiled assets.|
|
|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
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
If you run `rake routes`, you'll see that all the routes for the
|
If you run `rake routes`, you'll see that it has defined routes for all the
|
||||||
standard RESTful actions.
|
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
|
```bash
|
||||||
$ rake routes
|
$ rake routes
|
||||||
|
Prefix Verb URI Pattern Controller#Action
|
||||||
posts GET /posts(.:format) posts#index
|
posts GET /posts(.:format) posts#index
|
||||||
POST /posts(.:format) posts#create
|
POST /posts(.:format) posts#create
|
||||||
new_post GET /posts/new(.:format) posts#new
|
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| %>
|
<%= 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.
|
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
|
||||||
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.
|
`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:
|
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.
|
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
|
The reason we can use this shorter, simpler `form_for` declaration
|
||||||
will be explained in [just a moment](/form_helpers.html#binding-a-form-to-an-object).
|
to stand in for either of the other forms is that `@post` is a *resource*
|
||||||
For now, let's update the `app/views/posts/new.html.erb` view to use this new partial, rewriting it
|
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:
|
completely:
|
||||||
|
|
||||||
```html+erb
|
```html+erb
|
||||||
|
@ -1289,11 +1315,11 @@ So first, we'll wire up the Post show template
|
||||||
<h2>Add a comment:</h2>
|
<h2>Add a comment:</h2>
|
||||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :commenter %><br />
|
<%= f.label :commenter %><br>
|
||||||
<%= f.text_field :commenter %>
|
<%= f.text_field :commenter %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :body %><br />
|
<%= f.label :body %><br>
|
||||||
<%= f.text_area :body %>
|
<%= f.text_area :body %>
|
||||||
</p>
|
</p>
|
||||||
<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>
|
<h2>Add a comment:</h2>
|
||||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :commenter %><br />
|
<%= f.label :commenter %><br>
|
||||||
<%= f.text_field :commenter %>
|
<%= f.text_field :commenter %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :body %><br />
|
<%= f.label :body %><br>
|
||||||
<%= f.text_area :body %>
|
<%= f.text_area :body %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
@ -1435,11 +1461,11 @@ following:
|
||||||
<h2>Add a comment:</h2>
|
<h2>Add a comment:</h2>
|
||||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :commenter %><br />
|
<%= f.label :commenter %><br>
|
||||||
<%= f.text_field :commenter %>
|
<%= f.text_field :commenter %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :body %><br />
|
<%= f.label :body %><br>
|
||||||
<%= f.text_area :body %>
|
<%= f.text_area :body %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
@ -1465,11 +1491,11 @@ create a file `app/views/comments/_form.html.erb` containing:
|
||||||
```html+erb
|
```html+erb
|
||||||
<%= form_for([@post, @post.comments.build]) do |f| %>
|
<%= form_for([@post, @post.comments.build]) do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :commenter %><br />
|
<%= f.label :commenter %><br>
|
||||||
<%= f.text_field :commenter %>
|
<%= f.text_field :commenter %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<%= f.label :body %><br />
|
<%= f.label :body %><br>
|
||||||
<%= f.text_area :body %>
|
<%= f.text_area :body %>
|
||||||
</p>
|
</p>
|
||||||
<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 %>
|
<% end %>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<br />
|
<br>
|
||||||
|
|
||||||
<%= link_to "New book", new_book_path %>
|
<%= 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
|
```html+erb
|
||||||
<%= form_for(zone) do |f| %>
|
<%= form_for(zone) do |f| %>
|
||||||
<p>
|
<p>
|
||||||
<b>Zone name</b><br />
|
<b>Zone name</b><br>
|
||||||
<%= f.text_field :name %>
|
<%= f.text_field :name %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
|
|
@ -326,7 +326,7 @@ The following shows how to replace use `Rack::Builder` instead of the Rails supp
|
||||||
config.middleware.clear
|
config.middleware.clear
|
||||||
```
|
```
|
||||||
|
|
||||||
<br />
|
<br>
|
||||||
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|
Loading…
Reference in a new issue