rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -15,7 +15,7 @@
|
|
15
15
|
</p>
|
16
16
|
<% end %>
|
17
17
|
<p>
|
18
|
-
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.
|
18
|
+
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.16/">http://guides.rubyonrails.org/v3.2.16/</a>.
|
19
19
|
</p>
|
20
20
|
<p>
|
21
21
|
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
@@ -129,7 +129,7 @@ Note that the `params` hash is actually an instance of `ActiveSupport::HashWithI
|
|
129
129
|
|
130
130
|
### JSON parameters
|
131
131
|
|
132
|
-
If you're writing a web service application, you might find yourself more comfortable accepting parameters in JSON format. Rails will automatically convert your parameters into the `params` hash, which you can access as you would normally.
|
132
|
+
If you're writing a web service application, you might find yourself more comfortable accepting parameters in JSON format. If the "Content-Type" header of your request is set to "application/json", Rails will automatically convert your parameters into the `params` hash, which you can access as you would normally.
|
133
133
|
|
134
134
|
So for example, if you are sending this JSON content:
|
135
135
|
|
@@ -209,7 +209,7 @@ class PeopleController < ActionController::Base
|
|
209
209
|
# Request reply.
|
210
210
|
def update
|
211
211
|
person = current_account.people.find(params[:id])
|
212
|
-
person.
|
212
|
+
person.update!(person_params)
|
213
213
|
redirect_to person
|
214
214
|
end
|
215
215
|
|
@@ -257,7 +257,7 @@ params.require(:log_entry).permit!
|
|
257
257
|
```
|
258
258
|
|
259
259
|
This will mark the `:log_entry` parameters hash and any subhash of it
|
260
|
-
permitted.
|
260
|
+
permitted. Extreme care should be taken when using `permit!` as it
|
261
261
|
will allow all current and future model attributes to be
|
262
262
|
mass-assigned.
|
263
263
|
|
@@ -346,11 +346,11 @@ Your application has a session for each user in which you can store small amount
|
|
346
346
|
|
347
347
|
All session stores use a cookie to store a unique ID for each session (you must use a cookie, Rails will not allow you to pass the session ID in the URL as this is less secure).
|
348
348
|
|
349
|
-
For most stores, this ID is used to look up the session data on the server, e.g. in a database table. There is one exception, and that is the default and recommended session store - the CookieStore - which stores all session data in the cookie itself (the ID is still available to you if you need it). This has the advantage of being very lightweight and it requires zero setup in a new application in order to use the session. The cookie data is cryptographically signed to make it tamper-proof
|
349
|
+
For most stores, this ID is used to look up the session data on the server, e.g. in a database table. There is one exception, and that is the default and recommended session store - the CookieStore - which stores all session data in the cookie itself (the ID is still available to you if you need it). This has the advantage of being very lightweight and it requires zero setup in a new application in order to use the session. The cookie data is cryptographically signed to make it tamper-proof. And it is also encrypted so anyone with access to it can't read its contents. (Rails will not accept it if it has been edited).
|
350
350
|
|
351
|
-
The CookieStore can store around 4kB of data
|
351
|
+
The CookieStore can store around 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data in the session is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error.
|
352
352
|
|
353
|
-
If your user sessions don't store critical data or don't need to be around for long periods (for instance if you just use the flash for messaging), you can consider using ActionDispatch::Session::CacheStore
|
353
|
+
If your user sessions don't store critical data or don't need to be around for long periods (for instance if you just use the flash for messaging), you can consider using `ActionDispatch::Session::CacheStore`. This will store sessions using the cache implementation you have configured for your application. The advantage of this is that you can use your existing cache infrastructure for storing sessions without requiring any additional setup or administration. The downside, of course, is that the sessions will be ephemeral and could disappear at any time.
|
354
354
|
|
355
355
|
Read more about session storage in the [Security Guide](security.html).
|
356
356
|
|
@@ -538,7 +538,7 @@ end
|
|
538
538
|
Cookies
|
539
539
|
-------
|
540
540
|
|
541
|
-
Your application can store small amounts of data on the client
|
541
|
+
Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the `cookies` method, which - much like the `session` - works like a hash:
|
542
542
|
|
543
543
|
```ruby
|
544
544
|
class CommentsController < ApplicationController
|
@@ -568,10 +568,10 @@ end
|
|
568
568
|
|
569
569
|
Note that while for session values you set the key to `nil`, to delete a cookie value you should use `cookies.delete(:key)`.
|
570
570
|
|
571
|
-
Rendering
|
571
|
+
Rendering XML and JSON data
|
572
572
|
---------------------------
|
573
573
|
|
574
|
-
ActionController makes it extremely easy to render `
|
574
|
+
ActionController makes it extremely easy to render `XML` or `JSON` data. If you've generated a controller using scaffolding, it would look something like this:
|
575
575
|
|
576
576
|
```ruby
|
577
577
|
class UsersController < ApplicationController
|
@@ -808,11 +808,11 @@ class AdminsController < ApplicationController
|
|
808
808
|
|
809
809
|
private
|
810
810
|
|
811
|
-
|
812
|
-
|
813
|
-
|
811
|
+
def authenticate
|
812
|
+
authenticate_or_request_with_http_digest do |username|
|
813
|
+
USERS[username]
|
814
|
+
end
|
814
815
|
end
|
815
|
-
end
|
816
816
|
end
|
817
817
|
```
|
818
818
|
|
@@ -839,13 +839,13 @@ class ClientsController < ApplicationController
|
|
839
839
|
|
840
840
|
private
|
841
841
|
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
842
|
+
def generate_pdf(client)
|
843
|
+
Prawn::Document.new do
|
844
|
+
text client.name, align: :center
|
845
|
+
text "Address: #{client.address}"
|
846
|
+
text "Email: #{client.email}"
|
847
|
+
end.render
|
848
|
+
end
|
849
849
|
end
|
850
850
|
```
|
851
851
|
|
@@ -907,6 +907,92 @@ Now the user can request to get a PDF version of a client just by adding ".pdf"
|
|
907
907
|
GET /clients/1.pdf
|
908
908
|
```
|
909
909
|
|
910
|
+
### Live Streaming of Arbitrary Data
|
911
|
+
|
912
|
+
Rails allows you to stream more than just files. In fact, you can stream anything
|
913
|
+
you would like in a response object. The `ActionController::Live` module allows
|
914
|
+
you to create a persistent connection with a browser. Using this module, you will
|
915
|
+
be able to send arbitrary data to the browser at specific points in time.
|
916
|
+
|
917
|
+
#### Incorporating Live Streaming
|
918
|
+
|
919
|
+
Including `ActionController::Live` inside of your controller class will provide
|
920
|
+
all actions inside of the controller the ability to stream data. You can mix in
|
921
|
+
the module like so:
|
922
|
+
|
923
|
+
```ruby
|
924
|
+
class MyController < ActionController::Base
|
925
|
+
include ActionController::Live
|
926
|
+
|
927
|
+
def stream
|
928
|
+
response.headers['Content-Type'] = 'text/event-stream'
|
929
|
+
100.times {
|
930
|
+
response.stream.write "hello world\n"
|
931
|
+
sleep 1
|
932
|
+
}
|
933
|
+
ensure
|
934
|
+
response.stream.close
|
935
|
+
end
|
936
|
+
end
|
937
|
+
```
|
938
|
+
|
939
|
+
The above code will keep a persistent connection with the browser and send 100
|
940
|
+
messages of `"hello world\n"`, each one second apart.
|
941
|
+
|
942
|
+
There are a couple of things to notice in the above example. We need to make
|
943
|
+
sure to close the response stream. Forgetting to close the stream will leave
|
944
|
+
the socket open forever. We also have to set the content type to `text/event-stream`
|
945
|
+
before we write to the response stream. This is because headers cannot be written
|
946
|
+
after the response has been committed (when `response.committed` returns a truthy
|
947
|
+
value), which occurs when you `write` or `commit` the response stream.
|
948
|
+
|
949
|
+
#### Example Usage
|
950
|
+
|
951
|
+
Let's suppose that you were making a Karaoke machine and a user wants to get the
|
952
|
+
lyrics for a particular song. Each `Song` has a particular number of lines and
|
953
|
+
each line takes time `num_beats` to finish singing.
|
954
|
+
|
955
|
+
If we wanted to return the lyrics in Karaoke fashion (only sending the line when
|
956
|
+
the singer has finished the previous line), then we could use `ActionController::Live`
|
957
|
+
as follows:
|
958
|
+
|
959
|
+
```ruby
|
960
|
+
class LyricsController < ActionController::Base
|
961
|
+
include ActionController::Live
|
962
|
+
|
963
|
+
def show
|
964
|
+
response.headers['Content-Type'] = 'text/event-stream'
|
965
|
+
song = Song.find(params[:id])
|
966
|
+
|
967
|
+
song.each do |line|
|
968
|
+
response.stream.write line.lyrics
|
969
|
+
sleep line.num_beats
|
970
|
+
end
|
971
|
+
ensure
|
972
|
+
response.stream.close
|
973
|
+
end
|
974
|
+
end
|
975
|
+
```
|
976
|
+
|
977
|
+
The above code sends the next line only after the singer has completed the previous
|
978
|
+
line.
|
979
|
+
|
980
|
+
#### Streaming Considerations
|
981
|
+
|
982
|
+
Streaming arbitrary data is an extremely powerful tool. As shown in the previous
|
983
|
+
examples, you can choose when and what to send across a response stream. However,
|
984
|
+
you should also note the following things:
|
985
|
+
|
986
|
+
* Each response stream creates a new thread and copies over the thread local
|
987
|
+
variables from the original thread. Having too many thread local variables can
|
988
|
+
negatively impact performance. Similarly, a large number of threads can also
|
989
|
+
hinder performance.
|
990
|
+
* Failing to close the response stream will leave the corresponding socket open
|
991
|
+
forever. Make sure to call `close` whenever you are using a response stream.
|
992
|
+
* WEBrick servers buffer all responses, and so including `ActionController::Live`
|
993
|
+
will not work. You must use a web server which does not automatically buffer
|
994
|
+
responses.
|
995
|
+
|
910
996
|
Log Filtering
|
911
997
|
-------------
|
912
998
|
|
@@ -962,9 +1048,9 @@ class ApplicationController < ActionController::Base
|
|
962
1048
|
|
963
1049
|
private
|
964
1050
|
|
965
|
-
|
966
|
-
|
967
|
-
|
1051
|
+
def record_not_found
|
1052
|
+
render text: "404 Not Found", status: 404
|
1053
|
+
end
|
968
1054
|
end
|
969
1055
|
```
|
970
1056
|
|
@@ -976,10 +1062,10 @@ class ApplicationController < ActionController::Base
|
|
976
1062
|
|
977
1063
|
private
|
978
1064
|
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
1065
|
+
def user_not_authorized
|
1066
|
+
flash[:error] = "You don't have access to this section."
|
1067
|
+
redirect_to :back
|
1068
|
+
end
|
983
1069
|
end
|
984
1070
|
|
985
1071
|
class ClientsController < ApplicationController
|
@@ -993,10 +1079,10 @@ class ClientsController < ApplicationController
|
|
993
1079
|
|
994
1080
|
private
|
995
1081
|
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1082
|
+
# If the user is not authorized, just throw the exception.
|
1083
|
+
def check_authorization
|
1084
|
+
raise User::NotAuthorized unless current_user.admin?
|
1085
|
+
end
|
1000
1086
|
end
|
1001
1087
|
```
|
1002
1088
|
|
@@ -105,7 +105,7 @@ will be the template used for the email, formatted in HTML:
|
|
105
105
|
<h1>Welcome to example.com, <%= @user.name %></h1>
|
106
106
|
<p>
|
107
107
|
You have successfully signed up to example.com,
|
108
|
-
your username is: <%= @user.login %>.<br
|
108
|
+
your username is: <%= @user.login %>.<br>
|
109
109
|
</p>
|
110
110
|
<p>
|
111
111
|
To login to the site, just follow this link: <%= @url %>.
|
@@ -378,7 +378,7 @@ Just like with controller views, use `yield` to render the view inside the
|
|
378
378
|
layout.
|
379
379
|
|
380
380
|
You can also pass in a `layout: 'layout_name'` option to the render call inside
|
381
|
-
the format block to specify different layouts for different
|
381
|
+
the format block to specify different layouts for different formats:
|
382
382
|
|
383
383
|
```ruby
|
384
384
|
class UserMailer < ActionMailer::Base
|
@@ -481,7 +481,7 @@ end
|
|
481
481
|
|
482
482
|
There may be cases in which you want to skip the template rendering step and
|
483
483
|
supply the email body as a string. You can achieve this using the `:body`
|
484
|
-
option.
|
484
|
+
option. In such cases don't forget to add the `:content_type` option. Rails
|
485
485
|
will default to `text/plain` otherwise.
|
486
486
|
|
487
487
|
```ruby
|
@@ -569,25 +569,25 @@ class UserMailer < ActionMailer::Base
|
|
569
569
|
|
570
570
|
private
|
571
571
|
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
572
|
+
def set_delivery_options
|
573
|
+
# You have access to the mail instance,
|
574
|
+
# @business and @user instance variables here
|
575
|
+
if @business && @business.has_smtp_settings?
|
576
|
+
mail.delivery_method.settings.merge!(@business.smtp_settings)
|
577
|
+
end
|
577
578
|
end
|
578
|
-
end
|
579
579
|
|
580
|
-
|
581
|
-
|
582
|
-
|
580
|
+
def prevent_delivery_to_guests
|
581
|
+
if @user && @user.guest?
|
582
|
+
mail.perform_deliveries = false
|
583
|
+
end
|
583
584
|
end
|
584
|
-
end
|
585
585
|
|
586
|
-
|
587
|
-
|
588
|
-
|
586
|
+
def set_business_headers
|
587
|
+
if @business
|
588
|
+
headers["X-SMTPAPI-CATEGORY"] = @business.code
|
589
|
+
end
|
589
590
|
end
|
590
|
-
end
|
591
591
|
end
|
592
592
|
```
|
593
593
|
|
@@ -608,7 +608,7 @@ files (environment.rb, production.rb, etc...)
|
|
608
608
|
| Configuration | Description |
|
609
609
|
|---------------|-------------|
|
610
610
|
|`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.|
|
611
|
-
|`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`
|
611
|
+
|`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>|
|
612
612
|
|`sendmail_settings`|Allows you to override options for the `:sendmail` delivery method.<ul><li>`:location` - The location of the sendmail executable. Defaults to `/usr/sbin/sendmail`.</li><li>`:arguments` - The command line arguments to be passed to sendmail. Defaults to `-i -t`.</li></ul>|
|
613
613
|
|`raise_delivery_errors`|Whether or not errors should be raised if the email fails to be delivered. This only works if the external email server is configured for immediate delivery.|
|
614
614
|
|`delivery_method`|Defines a delivery method. Possible values are `:smtp` (default), `:sendmail`, `:file` and `:test`.|
|
@@ -634,7 +634,7 @@ config.action_mailer.delivery_method = :sendmail
|
|
634
634
|
# }
|
635
635
|
config.action_mailer.perform_deliveries = true
|
636
636
|
config.action_mailer.raise_delivery_errors = true
|
637
|
-
config.action_mailer.default_options = {from: 'no-
|
637
|
+
config.action_mailer.default_options = {from: 'no-reply@example.com'}
|
638
638
|
```
|
639
639
|
|
640
640
|
### Action Mailer Configuration for Gmail
|
@@ -68,7 +68,7 @@ Consider the following loop for names:
|
|
68
68
|
```html+erb
|
69
69
|
<h1>Names of all the people</h1>
|
70
70
|
<% @people.each do |person| %>
|
71
|
-
Name: <%= person.name %><br
|
71
|
+
Name: <%= person.name %><br>
|
72
72
|
<% end %>
|
73
73
|
```
|
74
74
|
|
@@ -152,7 +152,7 @@ By default, Rails will compile each template to a method in order to render it.
|
|
152
152
|
|
153
153
|
### Partials
|
154
154
|
|
155
|
-
Partial templates
|
155
|
+
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With partials, you can extract pieces of code from your templates to separate files and also reuse them throughout your templates.
|
156
156
|
|
157
157
|
#### Naming Partials
|
158
158
|
|
@@ -464,7 +464,7 @@ stylesheet_link_tag :monkey # =>
|
|
464
464
|
|
465
465
|
#### auto_discovery_link_tag
|
466
466
|
|
467
|
-
Returns a link tag that browsers and
|
467
|
+
Returns a link tag that browsers and feed readers can use to auto-detect an RSS or Atom feed.
|
468
468
|
|
469
469
|
```ruby
|
470
470
|
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "RSS Feed"}) # =>
|
@@ -775,8 +775,8 @@ select_day(5)
|
|
775
775
|
Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
|
776
776
|
|
777
777
|
```ruby
|
778
|
-
# Generates a select field for
|
779
|
-
|
778
|
+
# Generates a select field for hours that defaults to the hours for the time provided
|
779
|
+
select_hour(Time.now + 6.hours)
|
780
780
|
```
|
781
781
|
|
782
782
|
#### select_minute
|
@@ -941,9 +941,9 @@ Creates a form and a scope around a specific model object that is used as a base
|
|
941
941
|
```html+erb
|
942
942
|
<%= form_for @post do |f| %>
|
943
943
|
<%= f.label :title, 'Title' %>:
|
944
|
-
<%= f.text_field :title %><br
|
944
|
+
<%= f.text_field :title %><br>
|
945
945
|
<%= f.label :body, 'Body' %>:
|
946
|
-
<%= f.text_area :body %><br
|
946
|
+
<%= f.text_area :body %><br>
|
947
947
|
<% end %>
|
948
948
|
```
|
949
949
|
|
@@ -1006,6 +1006,24 @@ text_field(:post, :title)
|
|
1006
1006
|
# => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" />
|
1007
1007
|
```
|
1008
1008
|
|
1009
|
+
#### email_field
|
1010
|
+
|
1011
|
+
Returns an input tag of the "email" type tailored for accessing a specified attribute.
|
1012
|
+
|
1013
|
+
```ruby
|
1014
|
+
email_field(:user, :email)
|
1015
|
+
# => <input type="email" id="user_email" name="user[email]" value="#{@user.email}" />
|
1016
|
+
```
|
1017
|
+
|
1018
|
+
#### url_field
|
1019
|
+
|
1020
|
+
Returns an input tag of the "url" type tailored for accessing a specified attribute.
|
1021
|
+
|
1022
|
+
```ruby
|
1023
|
+
url_field(:user, :url)
|
1024
|
+
# => <input type="url" id="user_url" name="user[url]" value="#{@user.url}" />
|
1025
|
+
```
|
1026
|
+
|
1009
1027
|
### FormOptionsHelper
|
1010
1028
|
|
1011
1029
|
Provides a number of methods for turning different kinds of containers into a set of option tags.
|
@@ -1125,7 +1143,7 @@ Returns a string of option tags for pretty much any country in the world.
|
|
1125
1143
|
|
1126
1144
|
#### country_select
|
1127
1145
|
|
1128
|
-
|
1146
|
+
Returns select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
|
1129
1147
|
|
1130
1148
|
#### option_groups_from_collection_for_select
|
1131
1149
|
|
@@ -1224,15 +1242,23 @@ Returns a string of option tags for pretty much any time zone in the world.
|
|
1224
1242
|
|
1225
1243
|
#### time_zone_select
|
1226
1244
|
|
1227
|
-
|
1245
|
+
Returns select and option tags for the given object and method, using `time_zone_options_for_select` to generate the list of option tags.
|
1228
1246
|
|
1229
1247
|
```ruby
|
1230
1248
|
time_zone_select( "user", "time_zone")
|
1231
1249
|
```
|
1232
1250
|
|
1251
|
+
#### date_field
|
1252
|
+
|
1253
|
+
Returns an input tag of the "date" type tailored for accessing a specified attribute.
|
1254
|
+
|
1255
|
+
```ruby
|
1256
|
+
date_field("user", "dob")
|
1257
|
+
```
|
1258
|
+
|
1233
1259
|
### FormTagHelper
|
1234
1260
|
|
1235
|
-
Provides a number of methods for creating form tags that
|
1261
|
+
Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.
|
1236
1262
|
|
1237
1263
|
#### check_box_tag
|
1238
1264
|
|
@@ -1364,6 +1390,33 @@ text_field_tag 'name'
|
|
1364
1390
|
# => <input id="name" name="name" type="text" />
|
1365
1391
|
```
|
1366
1392
|
|
1393
|
+
#### email_field_tag
|
1394
|
+
|
1395
|
+
Creates a standard input field of email type.
|
1396
|
+
|
1397
|
+
```ruby
|
1398
|
+
email_field_tag 'email'
|
1399
|
+
# => <input id="email" name="email" type="email" />
|
1400
|
+
```
|
1401
|
+
|
1402
|
+
#### url_field_tag
|
1403
|
+
|
1404
|
+
Creates a standard input field of url type.
|
1405
|
+
|
1406
|
+
```ruby
|
1407
|
+
url_field_tag 'url'
|
1408
|
+
# => <input id="url" name="url" type="url" />
|
1409
|
+
```
|
1410
|
+
|
1411
|
+
#### date_field_tag
|
1412
|
+
|
1413
|
+
Creates a standard input field of date type.
|
1414
|
+
|
1415
|
+
```ruby
|
1416
|
+
date_field_tag "dob"
|
1417
|
+
# => <input id="dob" name="dob" type="date" />
|
1418
|
+
```
|
1419
|
+
|
1367
1420
|
### JavaScriptHelper
|
1368
1421
|
|
1369
1422
|
Provides functionality for working with JavaScript in your views.
|
@@ -1439,7 +1492,7 @@ number_to_human_size(1234567) # => 1.2 MB
|
|
1439
1492
|
Formats a number as a percentage string.
|
1440
1493
|
|
1441
1494
|
```ruby
|
1442
|
-
number_to_percentage(100, :
|
1495
|
+
number_to_percentage(100, precision: 0) # => 100%
|
1443
1496
|
```
|
1444
1497
|
|
1445
1498
|
#### number_to_phone
|
@@ -1467,12 +1520,78 @@ number_with_precision(111.2345) # => 111.235
|
|
1467
1520
|
number_with_precision(111.2345, 2) # => 111.23
|
1468
1521
|
```
|
1469
1522
|
|
1523
|
+
### SanitizeHelper
|
1524
|
+
|
1525
|
+
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
|
1526
|
+
|
1527
|
+
#### sanitize
|
1528
|
+
|
1529
|
+
This sanitize helper will html encode all tags and strip all attributes that aren't specifically allowed.
|
1530
|
+
|
1531
|
+
```ruby
|
1532
|
+
sanitize @article.body
|
1533
|
+
```
|
1534
|
+
|
1535
|
+
If either the :attributes or :tags options are passed, only the mentioned tags and attributes are allowed and nothing else.
|
1536
|
+
|
1537
|
+
```ruby
|
1538
|
+
sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style)
|
1539
|
+
```
|
1540
|
+
|
1541
|
+
To change defaults for multiple uses, for example adding table tags to the default:
|
1542
|
+
|
1543
|
+
```ruby
|
1544
|
+
class Application < Rails::Application
|
1545
|
+
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
|
1546
|
+
end
|
1547
|
+
```
|
1548
|
+
|
1549
|
+
#### sanitize_css(style)
|
1550
|
+
|
1551
|
+
Sanitizes a block of CSS code.
|
1552
|
+
|
1553
|
+
#### strip_links(html)
|
1554
|
+
Strips all link tags from text leaving just the link text.
|
1555
|
+
|
1556
|
+
```ruby
|
1557
|
+
strip_links("<a href="http://rubyonrails.org">Ruby on Rails</a>")
|
1558
|
+
# => Ruby on Rails
|
1559
|
+
```
|
1560
|
+
|
1561
|
+
```ruby
|
1562
|
+
strip_links("emails to <a href="mailto:me@email.com">me@email.com</a>.")
|
1563
|
+
# => emails to me@email.com.
|
1564
|
+
```
|
1565
|
+
|
1566
|
+
```ruby
|
1567
|
+
strip_links('Blog: <a href="http://myblog.com/">Visit</a>.')
|
1568
|
+
# => Blog: Visit.
|
1569
|
+
```
|
1570
|
+
|
1571
|
+
#### strip_tags(html)
|
1572
|
+
|
1573
|
+
Strips all HTML tags from the html, including comments.
|
1574
|
+
This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.
|
1575
|
+
|
1576
|
+
```ruby
|
1577
|
+
strip_tags("Strip <i>these</i> tags!")
|
1578
|
+
# => Strip these tags!
|
1579
|
+
```
|
1580
|
+
|
1581
|
+
```ruby
|
1582
|
+
strip_tags("<b>Bold</b> no more! <a href='more.html'>See more</a>")
|
1583
|
+
# => Bold no more! See more
|
1584
|
+
```
|
1585
|
+
|
1586
|
+
NB: The output may still contain unescaped '<', '>', '&' characters and confuse browsers.
|
1587
|
+
|
1588
|
+
|
1470
1589
|
Localized Views
|
1471
1590
|
---------------
|
1472
1591
|
|
1473
1592
|
Action View has the ability render different templates depending on the current locale.
|
1474
1593
|
|
1475
|
-
For example, suppose you have a
|
1594
|
+
For example, suppose you have a `PostsController` with a show action. By default, calling this action will render `app/views/posts/show.html.erb`. But if you set `I18n.locale = :de`, then `app/views/posts/show.de.html.erb` will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
|
1476
1595
|
|
1477
1596
|
You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
|
1478
1597
|
|