diff options
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/asset_pipeline.md | 6 | ||||
-rw-r--r-- | guides/source/association_basics.md | 6 | ||||
-rw-r--r-- | guides/source/command_line.md | 6 | ||||
-rw-r--r-- | guides/source/configuring.md | 41 | ||||
-rw-r--r-- | guides/source/engines.md | 8 | ||||
-rw-r--r-- | guides/source/getting_started.md | 16 | ||||
-rw-r--r-- | guides/source/i18n.md | 13 | ||||
-rw-r--r-- | guides/source/initialization.md | 2 | ||||
-rw-r--r-- | guides/source/migrations.md | 16 | ||||
-rw-r--r-- | guides/source/working_with_javascript_in_rails.md | 2 |
10 files changed, 93 insertions, 23 deletions
diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md index 7334e8b843..862742679c 100644 --- a/guides/source/asset_pipeline.md +++ b/guides/source/asset_pipeline.md @@ -682,10 +682,10 @@ the cached page. The default matcher for compiling files includes `application.js`, `application.css` and all non-JS/CSS files (this will include all image assets -automatically): +automatically) from `app/assets` folders including your gems: ```ruby -[ Proc.new { |path| !%w(.js .css).include?(File.extname(path)) }, +[ Proc.new { |path, fn| fn =~ /app\/assets/ && !%w(.js .css).include?(File.extname(path)) }, /application.(css|js)$/ ] ``` @@ -905,7 +905,7 @@ Customizing the Pipeline ### CSS Compression There is currently one option for compressing CSS, YUI. The [YUI CSS -compressor]((http://yui.github.io/yuicompressor/css.html) provides +compressor](http://yui.github.io/yuicompressor/css.html) provides minification. The following line enables YUI compression, and requires the `yui-compressor` diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 9b80a65a44..e133e71d42 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -1137,6 +1137,12 @@ Controls what happens to the associated object when its owner is destroyed: * `:restrict_with_exception` causes an exception to be raised if there is an associated record * `:restrict_with_error` causes an error to be added to the owner if there is an associated object +It's necessary not to set or leave `:nullify` option for those associations +that have `NOT NULL` database constraints. If you don't set `dependent` to +destroy such associations you won't be able to change the associated object +because initial associated object foreign key will be set to unallowed `NULL` +value. + ##### `:foreign_key` By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly: diff --git a/guides/source/command_line.md b/guides/source/command_line.md index 5f98326c57..639476eeeb 100644 --- a/guides/source/command_line.md +++ b/guides/source/command_line.md @@ -69,9 +69,9 @@ $ rails server => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server -[2012-05-28 00:39:41] INFO WEBrick 1.3.1 -[2012-05-28 00:39:41] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin11.2.0] -[2012-05-28 00:39:41] INFO WEBrick::HTTPServer#start: pid=69680 port=3000 +[2013-08-07 02:00:01] INFO WEBrick 1.3.1 +[2013-08-07 02:00:01] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin11.2.0] +[2013-08-07 02:00:01] INFO WEBrick::HTTPServer#start: pid=69680 port=3000 ``` With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open [http://localhost:3000](http://localhost:3000), you will see a basic Rails app running. diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 2f5444c763..0620849519 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -301,7 +301,7 @@ The schema dumper adds one additional configuration option: * `config.action_controller.allow_forgery_protection` enables or disables CSRF protection. By default this is `false` in test mode and `true` in all other modes. -* `config.action_controller.relative_url_root` can be used to tell Rails that you are deploying to a subdirectory. The default is `ENV['RAILS_RELATIVE_URL_ROOT']`. +* `config.action_controller.relative_url_root` can be used to tell Rails that you are [deploying to a subdirectory](configuring.html#deploy-to-a-subdirectory-relative-url-root). The default is `ENV['RAILS_RELATIVE_URL_ROOT']`. * `config.action_controller.permit_all_parameters` sets all the parameters for mass assignment to be permitted by default. The default value is `false`. @@ -535,6 +535,43 @@ Imagine you have a server which mirrors the production environment but is only u That environment is no different than the default ones, start a server with `rails server -e staging`, a console with `rails console staging`, `Rails.env.staging?` works, etc. +### Deploy to a subdirectory (relative url root) + +By default Rails expects that your application is running at the root +(eg. `/`). This section explains how to run your application inside a directory. + +Let's assume we want to deploy our application to "/app1". Rails needs to know +this directory to generate the appropriate routes: + +```ruby +config.relative_url_root = "/app1" +``` + +alternatively you can set the `RAILS_RELATIVE_URL_ROOT` environment +variable. + +Rails will now prepend "/app1" when generating links. + +#### Using Passenger + +Passenger makes it easiy to run your application in a subdirectory. You can find +the relevant configuration in the +[passenger manual](http://www.modrails.com/documentation/Users%20guide%20Apache.html#deploying_rails_to_sub_uri). + +#### Using a Reverse Proxy + +TODO + +#### Considerations when deploying to a subdirectory + +Deploying to a subdirectory in production has implications on various parts of +Rails. + +* development environment: +* testing environment: +* serving static assets: +* asset pipeline: + Rails Environment Settings -------------------------- @@ -542,7 +579,7 @@ Some parts of Rails can also be configured externally by supplying environment v * `ENV["RAILS_ENV"]` defines the Rails environment (production, development, test, and so on) that Rails will run under. -* `ENV["RAILS_RELATIVE_URL_ROOT"]` is used by the routing code to recognize URLs when you deploy your application to a subdirectory. +* `ENV["RAILS_RELATIVE_URL_ROOT"]` is used by the routing code to recognize URLs when you [deploy your application to a subdirectory](configuring.html#deploy-to-a-subdirectory-relative-url-root). * `ENV["RAILS_CACHE_ID"]` and `ENV["RAILS_APP_VERSION"]` are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application. diff --git a/guides/source/engines.md b/guides/source/engines.md index a77be917a2..9106b6382d 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -525,6 +525,14 @@ First, the `author_name` text field needs to be added to the `app/views/blorgh/p </div> ``` +Next, we need to update our `Blorgh::PostController#post_params` method to permit the new form parameter: + +```ruby +def post_params + params.require(:post).permit(:title, :text, :author_name) +end +``` + The `Blorgh::Post` model should then have some code to convert the `author_name` field into an actual `User` object and associate it as that post's `author` before the post is saved. It will also need to have an `attr_accessor` setup for this field so that the setter and getter methods are defined for it. To do all this, you'll need to add the `attr_accessor` for `author_name`, the association for the author and the `before_save` call into `app/models/blorgh/post.rb`. The `author` association will be hard-coded to the `User` class for the time being. diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index 12eb88f018..025e073d55 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -84,7 +84,7 @@ current version of Ruby installed: ```bash $ ruby -v -ruby 1.9.3p385 +ruby 2.0.0p247 ``` To install Rails, use the `gem install` command provided by RubyGems: @@ -310,6 +310,10 @@ end A controller is simply a class that is defined to inherit from `ApplicationController`. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system. +NOTE: There are `public`, `private` and `protected` methods in `Ruby` +(for more details you can check on [Programming Ruby](http://www.ruby-doc.org/docs/ProgrammingRuby/)). +But only `public` methods can be actions for controllers. + If you refresh <http://localhost:3000/posts/new> now, you'll get a new error: ![Unknown action new for PostsController!](images/getting_started/unknown_action_new_for_posts.png) @@ -575,7 +579,7 @@ If you submit the form again now, Rails will complain about not finding the `show` action. That's not very useful though, so let's add the `show` action before proceeding. -First we need to add a new `route` in `config/routes.rb`. +As we have seen in the output of `rake routes`, the route for `show` action is as follows: ```ruby post GET /posts/:id(.:format) posts#show @@ -641,7 +645,7 @@ private ``` See the `permit`? It allows us to accept both `title` and `text` in this -action. With this change, you should finally be able to create new `Post`s. +action. With this change, you should finally be able to create new posts. Visit <http://localhost:3000/posts/new> and give it a try! ![Show action for posts](images/getting_started/show_action_for_posts.png) @@ -654,7 +658,7 @@ For more information, refer to ### Listing all posts We still need a way to list all our posts, so let's do that. -We'll use a specific route from `config/routes.rb`: +The route for this as per output of `rake routes` is: ```ruby posts GET /posts(.:format) posts#index @@ -888,7 +892,7 @@ it look as follows: ```html+erb <h1>Editing post</h1> -<%= form_for :post, url: post_path(@post.id), method: :patch do |f| %> +<%= form_for :post, url: post_path(@post), method: :patch do |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited @@ -1071,7 +1075,7 @@ Then do the same for the `app/views/posts/edit.html.erb` view: We're now ready to cover the "D" part of CRUD, deleting posts from the database. Following the REST convention, the route for -deleting posts in the `config/routes.rb` is: +deleting posts as per output of `rake routes` is: ```ruby DELETE /posts/:id(.:format) posts#destroy diff --git a/guides/source/i18n.md b/guides/source/i18n.md index facfb96d98..e4214fd74e 100644 --- a/guides/source/i18n.md +++ b/guides/source/i18n.md @@ -731,6 +731,19 @@ en: Then `User.model_name.human` will return "Dude" and `User.human_attribute_name("login")` will return "Handle". +You can also set a plural form for model names, adding as following: + +```ruby +en: + activerecord: + models: + user: + one: Dude + other: Dudes +``` + +Then `User.model_name.human(:count => 2)` will return "Dudes". With `:count => 1` or without params will return "Dude". + #### Error Message Scopes Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes, and/or validations. It also transparently takes single table inheritance into account. diff --git a/guides/source/initialization.md b/guides/source/initialization.md index 26259408b4..c78eef5bf0 100644 --- a/guides/source/initialization.md +++ b/guides/source/initialization.md @@ -40,7 +40,7 @@ This file is as follows: ```ruby #!/usr/bin/env ruby APP_PATH = File.expand_path('../../config/application', __FILE__) -require File.expand_path('../../config/boot', __FILE__) +require_relative '../config/boot' require 'rails/commands' ``` diff --git a/guides/source/migrations.md b/guides/source/migrations.md index e6d1e71f5e..6100fc89c8 100644 --- a/guides/source/migrations.md +++ b/guides/source/migrations.md @@ -829,8 +829,7 @@ which contains a `Product` model: Bob goes on vacation. Alice creates a migration for the `products` table which adds a new column and -initializes it. She also adds a validation to the `Product` model for the new -column. +initializes it: ```ruby # db/migrate/20100513121110_add_flag_to_product.rb @@ -845,6 +844,8 @@ class AddFlagToProduct < ActiveRecord::Migration end ``` +She also adds a validation to the `Product` model for the new column: + ```ruby # app/models/product.rb @@ -853,9 +854,8 @@ class Product < ActiveRecord::Base end ``` -Alice adds a second migration which adds and initializes another column to the -`products` table and also adds a validation to the `Product` model for the new -column. +Alice adds a second migration which adds another column to the `products` +table and initializes it: ```ruby # db/migrate/20100515121110_add_fuzz_to_product.rb @@ -870,6 +870,8 @@ class AddFuzzToProduct < ActiveRecord::Migration end ``` +She also adds a validation to the `Product` model for the new column: + ```ruby # app/models/product.rb @@ -903,7 +905,7 @@ A fix for this is to create a local model within the migration. This keeps Rails from running the validations, so that the migrations run to completion. When using a local model, it's a good idea to call -`Product.reset_column_information` to refresh the `ActiveRecord` cache for the +`Product.reset_column_information` to refresh the Active Record cache for the `Product` model prior to updating data in the database. If Alice had done this instead, there would have been no problem: @@ -956,7 +958,7 @@ other product attributes. These migrations run just fine, but when Bob comes back from his vacation and calls `rake db:migrate` to run all the outstanding migrations, he gets a subtle bug: The descriptions have defaults, and the `fuzz` column is present, -but `fuzz` is nil on all products. +but `fuzz` is `nil` on all products. The solution is again to use `Product.reset_column_information` before referencing the Product model in a migration, ensuring the Active Record's diff --git a/guides/source/working_with_javascript_in_rails.md b/guides/source/working_with_javascript_in_rails.md index bd0c796673..301e0e7e6c 100644 --- a/guides/source/working_with_javascript_in_rails.md +++ b/guides/source/working_with_javascript_in_rails.md @@ -185,7 +185,7 @@ $(document).ready -> ``` Obviously, you'll want to be a bit more sophisticated than that, but it's a -start. +start. You can see more about the events [in the jquery-ujs wiki](https://github.com/rails/jquery-ujs/wiki/ajax). ### form_tag |