From ee4e7125a61c703332a8d591a0aee917ba828e62 Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Wed, 25 Apr 2012 15:43:10 +0200 Subject: Add partials section to getting started guide --- guides/source/getting_started.textile | 102 +++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 88d5ce6d9b..fae12f825d 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -835,7 +835,7 @@ Moving on, we need to add the +update+ action. The file +config/routes.rb+ will need just one more line: -put "posts/:id/update" +put "posts/:id" => "posts#update" And the +update+ action in +posts_controller+ itself should not look too complicated by now: @@ -930,6 +930,106 @@ 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. +Create a new file +app/views/posts/_form.html.erb+ with the following +content: + + +<%= form_for @post 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 %> +
+ +Everything except for the +form_for+ declaration remained the same. I'll +explain later how +form_for+ can figure out the right +action+ and ++method+ attributes when building the form, for now let's update the ++new+ and +edit+ views: + + +# app/views/posts/new.html.erb + +

New post

+ +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> + + +# app/views/posts/edit.html.erb + +

Edit post

+ +<%= render 'form' %> + +<%= link_to 'Back', :action => :index %> +
+ +Point your browser to +"http://localhost:3000/posts/new":http://localhost:3000/posts/new and +try creating a new post. Everything still works. Now try editing the +post and you'll receive the following error: + +!images/getting_started/undefined_method_post_path.png(Undefined method +post_path)! + +To understand this error, you need to understand how +form_for+ works. +When you pass an object to +form_for+ and you don't specify a +:url+ +option, Rails will try to guess the +action+ and +method+ options by +checking if the passed object is a new record or not. Rails follows the +REST convention, so to create a new +Post+ object it will look for a +route named +posts_path+, and to update a +Post+ object it will look for +a route named +post_path+ and pass the current object. Similarly, rails +knows that it should create new objects via POST and update them via +PUT. + +If you run +rake routes+ from the console you'll see that we already +have a +posts_path+ route, which was created automatically by Rails. +However, we don't have a +post_path+ yet, which is the reason why we +received an error before. + + +# rake routes + + posts GET /posts(.:format) posts#index + posts_new GET /posts/new(.:format) posts#new +posts_create POST /posts/create(.:format) posts#create + GET /posts/:id(.:format) posts#show + GET /posts/:id/edit(.:format) posts#edit + PUT /posts/:id(.:format) posts#update + root / welcome#index + + +To fix this, open +config/routes.rb+ and modify the +get "posts/:id"+ +line like this: + + +get "posts/:id" => "posts#show", :as => :post + + +Now you'll be able to update posts again. + h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3 From 7f04235e523253bcf0a7d58d9f9bb61313a83337 Mon Sep 17 00:00:00 2001 From: James Strocel Date: Wed, 25 Apr 2012 09:01:59 -0700 Subject: Copy Edit --- 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 e9d713d91d..2e9ab0526d 100644 --- a/guides/source/generators.textile +++ b/guides/source/generators.textile @@ -468,7 +468,7 @@ Replaces text inside a file. gsub_file 'name_of_file.rb', 'method.to_be_replaced', 'method.the_replacing_code' - Regular Expressions can be used to make this method more precise. You can also use append_file and prepend_file in the same way to place code at the beginning and end of a file respectively. -- cgit v1.2.3 From 047600157a1a7d2ec3e3afa7c90e539f94b37aa5 Mon Sep 17 00:00:00 2001 From: Les Nightingill Date: Wed, 25 Apr 2012 09:40:09 -0700 Subject: adds a mention of the fact that an engine's lib/assets directory is also on the load path --- guides/source/engines.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/engines.textile b/guides/source/engines.textile index 36210aedb0..71bcf6b713 100644 --- a/guides/source/engines.textile +++ b/guides/source/engines.textile @@ -695,7 +695,7 @@ If a template is rendered from within an engine and it's attempting to use one o h4. Assets -Assets within an engine work in an identical way to a full application. Because the engine class inherits from +Rails::Engine+, the application will know to look up in the engine's +app/assets+ directory for potential assets. +Assets within an engine work in an identical way to a full application. Because the engine class inherits from +Rails::Engine+, the application will know to look up in the engine's +app/assets+ and +lib/assets+ directories for potential assets. Much like all the other components of an engine, the assets should also be namespaced. This means if you have an asset called +style.css+, it should be placed at +app/assets/stylesheets/[engine name]/style.css+, rather than +app/assets/stylesheets/style.css+. If this asset wasn't namespaced, then there is a possibility that the host application could have an asset named identically, in which case the application's asset would take precedence and the engine's one would be all but ignored. -- cgit v1.2.3 From 8bf97d1a845385290f20f76ea1256712c2c4735e Mon Sep 17 00:00:00 2001 From: Jonathan Roes Date: Thu, 26 Apr 2012 15:47:48 -0300 Subject: Minor typo. --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index fae12f825d..d2127b973d 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -465,7 +465,7 @@ invoking the command: rake db:migrate RAILS_ENV=production. h4. Saving data in the controller -Back into +posts_controller+, we need to change the +create+ action +Back in +posts_controller+, we need to change the +create+ action to use the new +Post+ model to save data in the database. Open that file and change the +create+ action to look like the following: -- cgit v1.2.3 From 504e539166ab3528e6377ae8bcf5ddacb3572729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rory=20O=E2=80=99Kane?= Date: Thu, 26 Apr 2012 18:24:38 -0300 Subject: Active Support Core Extensions guide: reworded "on one hand" and similar to "for one thing" --- guides/source/active_support_core_extensions.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/active_support_core_extensions.textile b/guides/source/active_support_core_extensions.textile index 5d0a3f82e8..e4a6e145b9 100644 --- a/guides/source/active_support_core_extensions.textile +++ b/guides/source/active_support_core_extensions.textile @@ -1131,7 +1131,7 @@ h4. Output Safety h5. Motivation -Inserting data into HTML templates needs extra care. For example you can't just interpolate +@review.title+ verbatim into an HTML page. On one hand if the review title is "Flanagan & Matz rules!" the output won't be well-formed because an ampersand has to be escaped as "&amp;". On the other hand, depending on the application that may be a big security hole because users can inject malicious HTML setting a hand-crafted review title. Check out the "section about cross-site scripting in the Security guide":security.html#cross-site-scripting-xss for further information about the risks. +Inserting data into HTML templates needs extra care. For example, you can't just interpolate +@review.title+ verbatim into an HTML page. For one thing, if the review title is "Flanagan & Matz rules!" the output won't be well-formed because an ampersand has to be escaped as "&amp;". What's more, depending on the application, that may be a big security hole because users can inject malicious HTML setting a hand-crafted review title. Check out the "section about cross-site scripting in the Security guide":security.html#cross-site-scripting-xss for further information about the risks. h5. Safe Strings -- cgit v1.2.3 From 39574aa582337baa32a58caae7b2671cb52ae718 Mon Sep 17 00:00:00 2001 From: Bernat Foj Capell Date: Fri, 27 Apr 2012 11:12:40 +0200 Subject: find_by_sql actually triggers after_find --- guides/source/active_record_validations_callbacks.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/active_record_validations_callbacks.textile b/guides/source/active_record_validations_callbacks.textile index 88c4481e5e..f49d91fd3c 100644 --- a/guides/source/active_record_validations_callbacks.textile +++ b/guides/source/active_record_validations_callbacks.textile @@ -1064,6 +1064,7 @@ Additionally, the +after_find+ callback is triggered by the following finder met * +find_all_by_attribute+ * +find_by_attribute+ * +find_by_attribute!+ +* +find_by_sql+ * +last+ The +after_initialize+ callback is triggered every time a new object of the class is initialized. @@ -1076,7 +1077,6 @@ Just as with validations, it is also possible to skip callbacks. These methods s * +decrement_counter+ * +delete+ * +delete_all+ -* +find_by_sql+ * +increment+ * +increment_counter+ * +toggle+ -- cgit v1.2.3 From f4447607f20c420d9c341b19064c07d5b7aa6cee Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Fri, 27 Apr 2012 14:21:02 +0200 Subject: Add delete post section to Getting Started guide --- guides/source/getting_started.textile | 71 ++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index fae12f825d..0001fff389 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1017,7 +1017,7 @@ received an error before. posts_create POST /posts/create(.:format) posts#create GET /posts/:id(.:format) posts#show GET /posts/:id/edit(.:format) posts#edit - PUT /posts/:id(.:format) posts#update + PUT /posts/:id(.:format) posts#update root / welcome#index @@ -1030,6 +1030,75 @@ get "posts/:id" => "posts#show", :as => :post Now you'll be able to update posts again. +h4. Deleting Posts + +We're now ready to cover the "D" part of CRUD, deleting posts from the +database. Following the REST convention, we're going to add a route for +deleting posts: + + +# config/routes.rb + +delete "posts/:id" => "posts#destroy" + + +We use the +delete+ method for destroying resources, which is mapped to +the +destroy+ action, which is provided below: + + +# app/controllers/posts_controller.rb + +def destroy + @post = Post.find(params[:id]) + @post.destroy + + redirect_to :action => :index +end + + +You can call +destroy+ on Active Record objects when you want to delete +them from the dabase. Note that we don't need to add a view for this +action since we're redirecting to the +index+ action. + +Finally, add a 'destroy' link to your +index+ action to wrap everything +together. + + + + + + + + + + + +<% @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 %><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %>
+
+ +Here we're using +link_to+ in a different way. We wrap the ++:action+ and +:id+ attributes in a hash so that we can pass other +arguments to +link_to+. The +:method+ and +:confirm+ +options are used as html5 attributes so that when the click is linked, +Rails will first show a confirm dialog to the user, and then submit the +link with method +delete+. This is done via javascript automatically. + +!images/getting_started/confirm_dialog.png(Confirm Dialog)! + +Congratulations, you can now create, show, list, update and destroy +posts. In the next section will see how Rails can aid us when creating +REST applications, and how we can refactor our Blog app to take +advantage of it. + h4. Using the Console To see your validations in action, you can use the console. The console is a -- cgit v1.2.3