From 9e60cc2e48be47002a305f24fa5609df76647532 Mon Sep 17 00:00:00 2001 From: Robby Colvin Date: Sun, 22 Apr 2012 13:20:22 -0700 Subject: Adds favicon to guides layout --- guides/source/layout.html.erb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'guides/source') diff --git a/guides/source/layout.html.erb b/guides/source/layout.html.erb index 35b6fc7014..0a8daf7ae5 100644 --- a/guides/source/layout.html.erb +++ b/guides/source/layout.html.erb @@ -14,6 +14,8 @@ + + <% if @edge %> -- cgit v1.2.3 From 50c5005bafe7e43f81a141cd2c512379aec74325 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 23 Apr 2012 13:14:52 +1000 Subject: Make it explicit that Symbol-s are not officially supported. This will remove a lot of controversy. As an example, see https://github.com/ernie/squeel/issues/67#issuecomment-5270896 --- guides/source/active_record_querying.textile | 2 ++ 1 file changed, 2 insertions(+) (limited to 'guides/source') diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index 58eae2ee0f..c21b91a795 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -388,6 +388,8 @@ The field name can also be a string: Client.where('locked' => true) +NOTE: A `Symbol` value is not supported and should not be used. Always use a `String` instead: `Client.where(:status => 'active')`, but NOT `Client.where(:status => :active)`. + h5(#hash-range_conditions). Range Conditions The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section. -- cgit v1.2.3 From 50f79daf593c61e43efa8ce549fac56281210020 Mon Sep 17 00:00:00 2001 From: Dmytrii Nagirniak Date: Mon, 23 Apr 2012 13:45:22 +1000 Subject: Fix markdown in textile. --- guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index c21b91a795..34ff2b2714 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -388,7 +388,7 @@ The field name can also be a string: Client.where('locked' => true) -NOTE: A `Symbol` value is not supported and should not be used. Always use a `String` instead: `Client.where(:status => 'active')`, but NOT `Client.where(:status => :active)`. +NOTE: A +Symbol+ value is not supported and should not be used. Always use a +String+ instead: +Client.where(:status => 'active')+, but NOT +Client.where(:status => :active)+. h5(#hash-range_conditions). Range Conditions -- cgit v1.2.3 From 5acb345a86584e3c9c4b5a81c4499c767393c89a Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 24 Apr 2012 12:30:24 +0200 Subject: Fix some code in getting started guide --- guides/source/getting_started.textile | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index f184004f80..73f85ab8f9 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -516,7 +516,7 @@ end A couple of things to note. We use +Post.find+ to find the post we're interested in. We also use an instance variable (prefixed by +@+) to -hold our reference to the post object. We do this because Rails will pass all instance +hold a reference to the post object. We do this because Rails will pass all instance variables to the view. Now, create a new file +app/view/posts/show.html.erb+ with the following @@ -577,8 +577,8 @@ end h4. Adding links -You can now create, show, and list posts. But it's difficult to navigate -through pages, so let's add some links. +You can now create, show, and list posts. Now let's add some links to +navigate through pages. Open +app/views/welcome/index.html.erb+ and modify it as follows: @@ -657,11 +657,9 @@ controller by default. TIP: In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop -and restart the web server. +and restart the web server when a change is made. -Congratulations, you're riding the rails! Now it’s time to see how it all works. - -h4. The Model +h4. Adding Some Validation The model file, +app/models/post.rb+ is about as simple as it can get: @@ -676,8 +674,6 @@ your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another. -h4. Adding Some Validation - Rails includes methods to help you validate the data that you send to models. Open the +app/models/post.rb+ file and edit it: -- cgit v1.2.3 From f0de7172a6e1a52bf122578954d805a36a488d27 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 24 Apr 2012 14:21:07 +0200 Subject: Add update post section to getting started guide --- guides/source/getting_started.textile | 132 +++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 73f85ab8f9..478aa74f27 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -726,7 +726,7 @@ something went wrong. To do that, you'll modify +app/views/posts/index.html.erb+ to check for error messages: -<%= form_for :post do |f| %> +<%= form_for :post, :url => { :action => :create } do |f| %> <% if @post.errors.any? %>

<%= pluralize(@post.errors.count, "error") %> prohibited @@ -776,6 +776,136 @@ Now you'll get a nice error message when saving a post without title: !images/getting_started/form_with_errors.png(Form With Errors)! +h4. Updating Posts + +We've covered the "CR" part of CRUD. Now let's focus on the "U" part, +updating posts. + +The first step we'll take is adding a +edit+ action to ++posts_controller+. + +Start by adding a route to +config/routes.rb+: + + +get "posts/:id/edit" => "posts#edit" + + +And then add the controller action: + + +def edit + @post = Post.find(params[:id]) +end + + +The view will contain a form similar to the one we used when creating +new posts. Create a file called +app/views/posts/edit.html.erb+ and make +it look as follows: + + +

Editing post

+ +<%= form_for :post, :url => { :action => :update, :id => @post.id }, +:method => :put do |f| %> + <% if @post.errors.any? %> +
+

<%= pluralize(@post.errors.count, "error") %> prohibited + this post from being saved:

+
    + <% @post.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+ <% end %> +

+ <%= f.label :title %>
+ <%= f.text_field :title %> +

+ +

+ <%= f.label :text %>
+ <%= f.text_area :text %> +

+ +

+ <%= f.submit %> +

+<% end %> + +<%= link_to 'Back', :action => :index %> +
+ +This time we point the form to the +update+ action (not defined yet). +The +:method => :put+ option tells Rails that we want this form to be +submitted via +put+, which is the http method you're expected to use to +*update* resources according to the REST protocol. + +TIP: By default forms built with the +form_for_ helper are sent via +POST+. + +Moving on, we need to add the +update+ action. The file ++config/routes.rb+ will need just one more line: + + +put "posts/:id/update" + + +And the +update+ action in +posts_controller+ itself should not look too complicated by now: + + +def update + @post = Post.find(params[:id]) + + if @post.update_attributes(params[:post]) + redirect_to :action => :show, :id => @post.id + else + render 'edit' + end +end + + +The new method +update_attributes+ is used when you want to update a record +that already exists, and it accepts an hash containing the attributes +that you want to update. As before, if there was an error updating the +post we want to show the form back to the user. + +TIP: you don't need to pass all attributes to +update_attributes+. For +example, if you'd call +@post.update_attributes(:title => 'A new title') +Rails would only update the +title+ attribute, leaving all other +attributes untouched. + +Finally, we want to show a link to the +edit+ action in the +index+ and ++show+ views: + + +# app/view/posts/index.html.erb + + + + + + + + + +<% @posts.each do |post| %> + + + + + + +<% end %> +
TitleText
<%= post.title %><%= post.text %><%= link_to 'Show', :action => :show, :id => post.id %><%= link_to 'Edit', :action => :edit, :id => post.id %>
+ +# app/view/posts/show.html.erb + +... + +<%= link_to 'Back', :action => :index %> +| <%= link_to 'Edit', :action => :edit, :id => @post.id %> +
+ h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3 From f92814e4cc2a381fab7df374fbee5f86ebe91fa6 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Tue, 24 Apr 2012 14:22:19 +0200 Subject: make sample code more compact --- guides/source/getting_started.textile | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 478aa74f27..d52e3f65a7 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -619,19 +619,7 @@ Let's add links to the other views as well. # app/views/posts/new.html.erb <%= form_for :post do |f| %> -

- <%= f.label :title %>
- <%= f.text_field :title %> -

- -

- <%= f.label :text %>
- <%= f.text_area :text %> -

- -

- <%= f.submit %> -

+ ... <% end %> <%= link_to 'Back', :action => :index %> -- cgit v1.2.3 From f45f1bacb03421094ef3cca9cd541cc41798586c Mon Sep 17 00:00:00 2001 From: James Strocel Date: Tue, 24 Apr 2012 12:08:05 -0700 Subject: Added some useful methods to the generators.textile --- guides/source/generators.textile | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'guides/source') diff --git a/guides/source/generators.textile b/guides/source/generators.textile index 920ff997ae..836f3d8549 100644 --- a/guides/source/generators.textile +++ b/guides/source/generators.textile @@ -451,6 +451,27 @@ Adds a specified source to +Gemfile+: add_source "http://gems.github.com" +h4. +inject_into_file+ + +Injects a block of code into a defined position in your file. + + +inject_into_file 'name_of_file.rb', :after => "#The code goes below this line. Don't for get the Line break at the end\n" do <<-'RUBY' + puts "Hello World" +RUBY +end + + +h4. +gsub_file+ + +Replaces text inside a file. + + + gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code' + Date: Tue, 24 Apr 2012 16:47:44 -0300 Subject: Remove references to 'vendored plugins' except to note they used to exist but are deprecated. 'gemified plugin' is the only supported option now. --- guides/source/plugins.textile | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'guides/source') diff --git a/guides/source/plugins.textile b/guides/source/plugins.textile index 97b4eca779..95e38db483 100644 --- a/guides/source/plugins.textile +++ b/guides/source/plugins.textile @@ -25,16 +25,14 @@ endprologue. h3. Setup -Before you continue, take a moment to decide if your new plugin will be potentially shared across different Rails applications. +_"vendored plugins"_ were available in previous versions of Rails, but they are deprecated in +Rails 3.2, and will not be available in the future. -* If your plugin is specific to your application, your new plugin will be a _vendored plugin_. -* If you think your plugin may be used across applications, build it as a _gemified plugin_. +Currently, Rails plugins are built as gems, _gemified plugins_. They can be shared accross +different rails applications using RubyGems and Bundler if desired. h4. Generate a gemified plugin. -Writing your Rails plugin as a gem, rather than as a vendored plugin, - lets you share your plugin across different rails applications using - RubyGems and Bundler. Rails 3.1 ships with a +rails plugin new+ command which creates a skeleton for developing any kind of Rails extension with the ability -- cgit v1.2.3 From 15338530f8e82e335d940f2938f1b22ef986c5da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=A3is=20Ozols?= Date: Wed, 25 Apr 2012 08:04:23 +0300 Subject: for get -> forget --- guides/source/generators.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/generators.textile b/guides/source/generators.textile index 836f3d8549..ec5f53870e 100644 --- a/guides/source/generators.textile +++ b/guides/source/generators.textile @@ -456,7 +456,7 @@ h4. +inject_into_file+ Injects a block of code into a defined position in your file. -inject_into_file 'name_of_file.rb', :after => "#The code goes below this line. Don't for get the Line break at the end\n" do <<-'RUBY' +inject_into_file 'name_of_file.rb', :after => "#The code goes below this line. Don't forget the Line break at the end\n" do <<-'RUBY' puts "Hello World" RUBY end -- cgit v1.2.3 From 6f0929110464b40072b62cb1c55a0589059fe593 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 25 Apr 2012 12:18:55 +0200 Subject: Add screenshot to updating post section --- guides/source/getting_started.textile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index d52e3f65a7..78e9761570 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -858,7 +858,7 @@ that you want to update. As before, if there was an error updating the post we want to show the form back to the user. TIP: you don't need to pass all attributes to +update_attributes+. For -example, if you'd call +@post.update_attributes(:title => 'A new title') +example, if you'd call +@post.update_attributes(:title => 'A new title')+ Rails would only update the +title+ attribute, leaving all other attributes untouched. @@ -894,6 +894,11 @@ Finally, we want to show a link to the +edit+ action in the +index+ and | <%= link_to 'Edit', :action => :edit, :id => @post.id %> +And here's how our app looks so far: + +!images/getting_started/index_action_with_edit_link.png(Index action +with edit link)! + h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3 From ccbd32c294bf685a389d456164a12fd777756873 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 25 Apr 2012 13:18:59 +0200 Subject: Add partials explanation to getting started guide --- guides/source/getting_started.textile | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 78e9761570..88d5ce6d9b 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -899,6 +899,37 @@ And here's how our app looks so far: !images/getting_started/index_action_with_edit_link.png(Index action with edit link)! +h4. Using partials to clean up duplication in views + ++partials+ are what Rails uses to remove duplication in views. Here's a +simple example: + + +# app/views/user/show.html.erb + +

<%= @user.name %>

+ +<%= render 'user_details' %> + +# app/views/user/_user_details.html.erb + +<%= @user.location %> + +<%= @user.about_me %> +
+ +The +show+ view will automatically include the content of the ++_user_details+ view. Note that partials are prefixed by an underscore, +as to not be confused with regular views. However, you don't include the +underscore when including them with the +helper+ method. + +TIP: You can red more about partials in the "Layouts and Rendering in +Rails":layouts_and_rendering.html guide. + +Our +edit+ action looks very similar to the +new+ action, in fact they +both share the same code for displaying the form. Lets clean them up by +using a +_form+ partial. + h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3 From 8189536aeb91394ccc649b9c14f0cf15c25b9423 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Wed, 25 Apr 2012 18:30:21 +0530 Subject: copy edits [ci skip] --- guides/source/active_record_querying.textile | 2 +- guides/source/generators.textile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index 34ff2b2714..98937266ba 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -388,7 +388,7 @@ The field name can also be a string: Client.where('locked' => true)
-NOTE: A +Symbol+ value is not supported and should not be used. Always use a +String+ instead: +Client.where(:status => 'active')+, but NOT +Client.where(:status => :active)+. +NOTE: The values cannot be symbols. For example, you cannot do +Client.where(:status => :active)+. h5(#hash-range_conditions). Range Conditions diff --git a/guides/source/generators.textile b/guides/source/generators.textile index ec5f53870e..e9d713d91d 100644 --- a/guides/source/generators.textile +++ b/guides/source/generators.textile @@ -467,7 +467,7 @@ h4. +gsub_file+ Replaces text inside a file. - gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code' +gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code'