From 533bcd75d392cd1e0a9fcc5976237e877953477d Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 10:50:57 +1000 Subject: [getting started] Split up creation of route, action and view for PostsController#index --- guides/source/getting_started.textile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index e93a94448a..f449cc9679 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -560,19 +560,21 @@ be able to create a post. Try it! h4. Listing all posts We still need a way to list all our posts, so let's do that. As usual, -we'll need a route, a controller action, and a view: +we'll need a route placed into +config/routes.rb+: -# Add to config/routes.rb get "posts" => "posts#index" + + +And an action for that route inside the +PostsController+ in the +app/controllers/posts_controller.rb+ file: -# Add to app/controllers/posts_controller.rb + def index @posts = Post.all end -+app/view/posts/index.html.erb+: +And then finally a view for this action, located at +app/views/posts/index.html.erb+:

Listing posts

-- cgit v1.2.3 From af2a6ac8539376d42f2d419f3e5bd4081a2ddc22 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 10:51:11 +1000 Subject: [getting started] indent iteration in posts/index template --- guides/source/getting_started.textile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index f449cc9679..142c5f8a70 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -585,12 +585,12 @@ And then finally a view for this action, located at +app/views/posts/index.html. Text -<% @posts.each do |post| %> - - <%= post.title %> - <%= post.text %> - -<% end %> + <% @posts.each do |post| %> + + <%= post.title %> + <%= post.text %> + + <% end %>
-- cgit v1.2.3 From ca18e2f8dd742107a5f7eee9de584ab858527b97 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 11:02:49 +1000 Subject: [getting started] add segue at the end of posts/index section --- guides/source/getting_started.textile | 2 ++ 1 file changed, 2 insertions(+) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 142c5f8a70..9870b157d8 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -594,6 +594,8 @@ And then finally a view for this action, located at +app/views/posts/index.html. +Now if you go to +http://localhost:3000/posts+ you will see a list of all the posts that you have created. + h4. Adding links You can now create, show, and list posts. Now let's add some links to -- cgit v1.2.3 From aa7f2a3564aeae17adfbf0c8f40fe38b072c7d52 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:47:21 +1000 Subject: [getting started] split up addition of links to index/show/new area --- guides/source/getting_started.textile | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 9870b157d8..d718ba409a 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -612,41 +612,25 @@ The +link_to+ method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts. -Let's add links to the other views as well. +Let's add links to the other views as well, starting with adding this "New Post" link to +app/views/posts/index.html.erb+, placing it above the ++ tag: -# app/views/posts/index.html.erb - -

Listing posts

- <%= link_to 'New post', :action => :new %> +
-
- - - - - - -<% @posts.each do |post| %> - - - - - -<% end %> -
TitleText
<%= post.title %><%= post.text %><%= link_to 'Show', :action => :show, :id => post.id %>
- -# app/views/posts/new.html.erb +This link will allow you to bring up the form that lets you create a new post. You should also add a link to this template -- +app/views/posts/new.html.erb+ -- to go back to the +index+ action. Do this by adding this underneath the form in this template: + <%= form_for :post do |f| %> ... <% end %> <%= link_to 'Back', :action => :index %> + -# app/views/posts/show.html.erb +Finally, add another link to the +app/views/posts/show.html.erb+ template to go back to the +index+ action as well, so that people who are viewing a single post can go back and view the whole list again: +

Title: <%= @post.title %> -- cgit v1.2.3 From 9a8c91d8e8d958eed5c0854517428104f585149b Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:47:34 +1000 Subject: [getting started] add attr_accessible to Post model --- guides/source/getting_started.textile | 2 ++ 1 file changed, 2 insertions(+) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index d718ba409a..c33e92c547 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -672,6 +672,8 @@ Open the +app/models/post.rb+ file and edit it: class Post < ActiveRecord::Base + attr_accessible :text, :title + validates :title, :presence => true, :length => { :minimum => 5 } end -- cgit v1.2.3 From 926e160ed0292240251c481e6fdef3ea14e139bc Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:47:53 +1000 Subject: [getting started] Explain the create action check for validations better --- guides/source/getting_started.textile | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index c33e92c547..0d247d6299 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -685,15 +685,9 @@ format, and the existence of associated objects. Validations are covered in deta in "Active Record Validations and Callbacks":active_record_validations_callbacks.html#validations-overview -If you open +posts_controller+ again, you'll notice that we don't check -the result of calling +@post.save+. We need to change its behavior to -show the form back to the user if any error occur: +With the validation now in place, when you call +@post.save+ on an invalid post, it will return +false+. If you open +app/controllers/posts_controller.rb+ again, you'll notice that we don't check the result of calling +@post.save+ inside the +create+ action. If +@post.save+ fails in this situation, we need to show the form back to the user. To do this, change the +create+ action inside +app/controllers/posts_controller.rb+ to this: -def new - @post = Post.new -end - def create @post = Post.new(params[:post]) @@ -705,13 +699,8 @@ def create end -Notice that I've also added +@post = Post.new+ to the +new+ action. I'll -explain why I did that in the next section, for now add that to your -controller as well. - -Also notice that we use +render+ instead of +redirect_to+ when +save+ -returns false. We can use +render+ so that the +@post+ object is passed -back to the view. +Notice that we use +render+ instead of +redirect_to+ when +save+ +returns +false+. The +render+ method is used so that the +@post+ object is passed back to the +new+ template when it is rendered. This rendering is done within the same request as the form submission, whereas the +redirect_to+ will tell the browser to issue another request. If you reload "http://localhost:3000/posts/new":http://localhost:3000/posts/new and -- cgit v1.2.3 From e91a1a0321bd12968cba3cfa504119e74c1caa2c Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:50:46 +1000 Subject: [getting started] re-add new action back to PostsController, explain that it'll be explained in a short while --- guides/source/getting_started.textile | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 0d247d6299..5c418e3af6 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -685,9 +685,18 @@ format, and the existence of associated objects. Validations are covered in deta in "Active Record Validations and Callbacks":active_record_validations_callbacks.html#validations-overview -With the validation now in place, when you call +@post.save+ on an invalid post, it will return +false+. If you open +app/controllers/posts_controller.rb+ again, you'll notice that we don't check the result of calling +@post.save+ inside the +create+ action. If +@post.save+ fails in this situation, we need to show the form back to the user. To do this, change the +create+ action inside +app/controllers/posts_controller.rb+ to this: +With the validation now in place, when you call +@post.save+ on an invalid +post, it will return +false+. If you open +app/controllers/posts_controller.rb+ +again, you'll notice that we don't check the result of calling +@post.save+ +inside the +create+ action. If +@post.save+ fails in this situation, we need to +show the form back to the user. To do this, change the +new+ and +create+ +actions inside +app/controllers/posts_controller.rb+ to these: +def new + @post = Post.new +end + def create @post = Post.new(params[:post]) @@ -699,7 +708,10 @@ def create end -Notice that we use +render+ instead of +redirect_to+ when +save+ +The +new+ action is now creating a new instance variable called +@post+, and +you'll see why that is in just a few moments. + +Notice that inside the +create+ action we use +render+ instead of +redirect_to+ when +save+ returns +false+. The +render+ method is used so that the +@post+ object is passed back to the +new+ template when it is rendered. This rendering is done within the same request as the form submission, whereas the +redirect_to+ will tell the browser to issue another request. If you reload -- cgit v1.2.3 From 10dfbaa9db6c54e89468a41c24dbbecf58fc430c Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:51:03 +1000 Subject: [getting started] fix weird linebreak when talking about pluralize --- guides/source/getting_started.textile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 5c418e3af6..aad6a30705 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -756,9 +756,8 @@ A few things are going on. We check if there are any errors with +@post.errors.any?+, and in that case we show a list of all errors with +@post.errors.full_messages+. -+pluralize+ is a rails helper -that takes a number and a string as its arguments. If the number is -greater than one, the string will be automatically pluralized. ++pluralize+ is a rails helper that takes a number and a string as its +arguments. If the number is greater than one, the string will be automatically pluralized. The reason why we added +@post = Post.new+ in +posts_controller+ is that otherwise +@post+ would be +nil+ in our view, and calling -- cgit v1.2.3 From 9073cf85dbab49fe845225b62129929938f0c7c9 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:52:10 +1000 Subject: [getting started] some more handholding around where exactly the error would appear when a post is invalid --- guides/source/getting_started.textile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index aad6a30705..5c5e73c1d1 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -767,7 +767,8 @@ TIP: Rails automatically wraps fields that contain an error with a div with class +field_with_errors+. You can define a css rule to make them standout. -Now you'll get a nice error message when saving a post without title: +Now you'll get a nice error message when saving a post without title when you +attempt to do just that on the "new post form(http://localhost:3000/posts/new)":http://localhost:3000/posts/new. !images/getting_started/form_with_errors.png(Form With Errors)! -- cgit v1.2.3 From 9c76aa0cc57aacb9f719bfb8aaeac5b7ad1711a5 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 17:57:24 +1000 Subject: [getting started] grammatical changes at beginning of update action section --- guides/source/getting_started.textile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 5c5e73c1d1..41dd499717 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -832,21 +832,23 @@ it look as follows: <%= link_to 'Back', :action => :index %> -This time we point the form to the +update+ action (not defined yet). +This time we point the form to the +update+ action, which is not defined yet +but will be very soon. + 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 +submitted via the +PUT+, HTTP method 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 +Next, we need to add the +update+ action. The file +config/routes.rb+ will need just one more line: put "posts/:id" => "posts#update" -And the +update+ action in +posts_controller+ itself should not look too complicated by now: +And then create the +update+ action in +app/controllers/posts_controller.rb+: def update @@ -860,7 +862,7 @@ def update end -The new method +update_attributes+ is used when you want to update a record +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. -- cgit v1.2.3 From f76e0a5484c7d4ee202c479309f3da6992ddf4bd Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:01:50 +1000 Subject: [getting started] split up adding link for edit to index and show templates --- guides/source/getting_started.textile | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 41dd499717..8a0ade1488 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -872,11 +872,11 @@ 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: +Finally, we want to show a link to the +edit+ action in the list of all the +posts, so let's add that now to +app/views/posts/index.html.erb+ to make it +appear next to the "Show" link: -# app/view/posts/index.html.erb @@ -895,11 +895,16 @@ Finally, we want to show a link to the +edit+ action in the +index+ and <% end %>
+
-# app/view/posts/show.html.erb +And we'll also add one to the +app/views/posts/show.html.erb+ template as well, +so that there's also an "Edit" link on a post's page. Add this at the bottom of +the template: + ... + <%= link_to 'Back', :action => :index %> | <%= link_to 'Edit', :action => :edit, :id => @post.id %> -- cgit v1.2.3 From 038af99a0797f336e3521ae336e86e60d430eb73 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:02:22 +1000 Subject: [getting started] Prefix users templates with 'users' to show that's where they're coming from --- guides/source/getting_started.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 8a0ade1488..9b6f7ed9dd 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -933,8 +933,8 @@ simple example: <%= @user.about_me %> -The +show+ view will automatically include the content of the -+_user_details+ view. Note that partials are prefixed by an underscore, +The +users/show+ template will automatically include the content of the ++users/_user_details+ template. 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. -- cgit v1.2.3 From 0c90bf058ac3f0c9d26e19e884c320655f520a94 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:02:55 +1000 Subject: [getting started] remove tab in form partial --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 9b6f7ed9dd..3106c2ffa2 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -953,7 +953,7 @@ content: <% if @post.errors.any? %>

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

+ this post from being saved:
    <% @post.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • -- cgit v1.2.3 From dd3ee84cd842a11be253e2fe2f29b45232fdb2c6 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:10:39 +1000 Subject: [getting started] don't call it 'a _form partial', just 'a partial' --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 3106c2ffa2..0456abebb7 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -943,7 +943,7 @@ 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. +using a partial. Create a new file +app/views/posts/_form.html.erb+ with the following content: -- cgit v1.2.3 From 8cfde229ddfbc22d83a84c46c423eefbe49bb694 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:10:55 +1000 Subject: [getting started] Split up refactoring of new and edit templates --- guides/source/getting_started.textile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 0456abebb7..40c3303b07 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -977,23 +977,23 @@ content: <% 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: +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 will be explained in just a moment. For now, let's update the ++app/views/posts/new.html.erb+ view to use this new partial, rewriting it +completely: -# app/views/posts/new.html.erb -

    New post

    <%= render 'form' %> <%= link_to 'Back', :action => :index %> +
    +Then do the same for the +app/views/posts/edit.html.erb+ view: -# app/views/posts/edit.html.erb - +

    Edit post

    <%= render 'form' %> -- cgit v1.2.3 From 950a7b40af1b32df17dcb9c9304b1f48f4e8fafc Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Wed, 16 May 2012 18:11:21 +1000 Subject: [getting started] explain that posts_path exists due to how we defined routes earlier --- guides/source/getting_started.textile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 40c3303b07..8f752524e9 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1001,8 +1001,7 @@ Then do the same for the +app/views/posts/edit.html.erb+ view: <%= link_to 'Back', :action => :index %>
    -Point your browser to -"http://localhost:3000/posts/new":http://localhost:3000/posts/new and +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: @@ -1020,7 +1019,8 @@ 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. +have a +posts_path+ route, which was created automatically by Rails when we +defined the route for the index action. However, we don't have a +post_path+ yet, which is the reason why we received an error before. -- cgit v1.2.3 From 146105a40f3a55041a618099bd49a66a335bb20c Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 17 May 2012 10:25:59 +0200 Subject: explain why we also add attr_accessible in the model --- guides/source/getting_started.textile | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 8f752524e9..c105007a1a 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -652,7 +652,7 @@ 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 when a change is made. -h4. Adding Some Validation +h4. Allowing the update of fields The model file, +app/models/post.rb+ is about as simple as it can get: @@ -667,6 +667,21 @@ 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. +Rails includes methods to help you secure some of your model fields. +Open the +app/models/post.rb+ file and edit it: + + +class Post < ActiveRecord::Base + attr_accessible :text, :title +end + + +This change will ensure that all changes made through HTML forms can edit the content of the text and title fields. +It will not be possible to define any other field value through forms. You can still define them by calling the `field=` method of course. +Accessible attributes and the mass assignment probem is covered in details in the "Security guide":security.html#mass-assignment + +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 53aaf95855b3e9bdcc834e906042c7dc0ca446c3 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:07:24 +1000 Subject: [getting started] explain :as option --- guides/source/getting_started.textile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index c105007a1a..166de11deb 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1058,7 +1058,10 @@ line like this: get "posts/:id" => "posts#show", :as => :post -Now you'll be able to update posts again. +The +:as+ option tells the +get+ method that we want to make routing helpers +called +post_url+ and +post_path+ available to our application. These are +precisely the methods that the +form_for+ needs when editing a post, and so now +you'll be able to update posts again. h4. Deleting Posts -- cgit v1.2.3 From 00dce8500952256965723697bb308d0aea7719fd Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:11:36 +1000 Subject: [getting started guide] more explanation around the delete routing method: --- guides/source/getting_started.textile | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 166de11deb..6051b43bd3 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1063,24 +1063,32 @@ called +post_url+ and +post_path+ available to our application. These are precisely the methods that the +form_for+ needs when editing a post, and so now you'll be able to update posts again. +NOTE: The +:as+ option is available on the +post+, +put+, +delete+ and +match+ +routing methods also. + 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: +deleting posts to +config/routes.rb+: -# 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: +The +delete+ routing method should be used for routes that destroy +resources. If this was left as a typical +get+ route, it could be possible for +people to craft malicious URLs like this: - -# app/controllers/posts_controller.rb + +look at this cat! + + +We use the +delete+ method for destroying resources, and this route is mapped to +the +destroy+ action inside +app/controllers/posts_controller.rb+, which doesn't exist yet, but is +provided below: + def destroy @post = Post.find(params[:id]) @post.destroy -- cgit v1.2.3 From a99417be2d28a703e9ffc45503d48dbdbfb1e1ef Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:11:47 +1000 Subject: [getting started] dabase -> database --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 6051b43bd3..457d0caa9d 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1098,7 +1098,7 @@ 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 +them from the database. 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 -- cgit v1.2.3 From 9fc5b456a000f5fada0edb7b483b14915fd90b83 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:16:47 +1000 Subject: [getting started] Fix index template after destroy action addition --- guides/source/getting_started.textile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 457d0caa9d..87e3aea2aa 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1101,10 +1101,12 @@ You can call +destroy+ on Active Record objects when you want to delete them from the database. 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 +Finally, add a 'destroy' link to your +index+ action template +(+app/views/posts/index.html.erb) to wrap everything together. +

    Listing Posts

    -- cgit v1.2.3 From 42c6f89c5dfa59ad2f91eb6f9f9da8f49999b03e Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:17:17 +1000 Subject: [getting started] dialog box is not automatic, is provided through jquery_ujs, which is necessary to have in application layout --- guides/source/getting_started.textile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 87e3aea2aa..4aeba5debe 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1129,11 +1129,14 @@ together. 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+ ++:action+ and +:id+ attributes in a hash so that we can pass those two keys in +first as one argument, and then the final two keys as another argument. 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. +link with method +delete+. This is done via the JavaScript file +jquery_ujs+ +which is automatically included into your application's layout +(+app/views/layouts/application.html.erb+) when you generated the application. +Without this file, the confirmation dialog box wouldn't appear. !images/getting_started/confirm_dialog.png(Confirm Dialog)! -- cgit v1.2.3 From cc9a6f2e0ae6b4691d52b61167c31d2383759ba1 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:17:49 +1000 Subject: [getting started] split up section about rake routes after 'resources' call --- guides/source/getting_started.textile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 4aeba5debe..1a57a81cf1 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1176,7 +1176,7 @@ end If you run +rake routes+, you'll see that all the routes that we -declared before are still available, and the app still works as before. +declared before are still available: # rake routes @@ -1190,6 +1190,9 @@ edit_post GET /posts/:id/edit(.:format) posts#edit root / welcome#index +Also, if you go through the motions of creating, updating and deleting +posts the app still works as before. + TIP: In general, Rails encourages the use of resources objects in place of declaring routes manually. For more information about routing, see "Rails Routing from the Outside In":routing.html. -- cgit v1.2.3 From a4db311da0db9b87e12eebfc49b0d97a3af35043 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 19:18:00 +1000 Subject: [getting started] generating routes manually was done as a learning exercise, fix typos --- guides/source/getting_started.textile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 1a57a81cf1..4680cadacf 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1194,17 +1194,18 @@ Also, if you go through the motions of creating, updating and deleting posts the app still works as before. TIP: In general, Rails encourages the use of resources objects in place -of declaring routes manually. For more information about routing, see +of declaring routes manually. It was only done in this guide as a learning +exercise. For more information about routing, see "Rails Routing from the Outside In":routing.html. h3. Adding a Second Model It's time to add a second model to the application. The second model will handle comments on -blog posts. +posts. h4. Generating a Model -We're going to se the same generator that we used before when creating +We're going to see the same generator that we used before when creating the +Post+ model. This time we'll create a +Comment+ model to hold reference of post comments. Run this command in your terminal: -- cgit v1.2.3 From 2c2d70eb95c5c02e0e380bc3dc8a8837fbf62f15 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 20:24:15 +1000 Subject: [getting started] Remove beginning ruby link in posts/show --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 4680cadacf..ad31f7ce3b 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1365,7 +1365,7 @@ So first, we'll wire up the Post show template

    Text: - <%= @post.texthttp://beginningruby.org/ %> + <%= @post.text %>

    Add a comment:

    -- cgit v1.2.3 From a9f029ebf0229f3383b5d8853f6d7293e14ae512 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 20:27:15 +1000 Subject: [getting started] briefly explain form_for with array parameters --- guides/source/getting_started.textile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index ad31f7ce3b..8463887cfd 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1388,7 +1388,10 @@ So first, we'll wire up the Post show template This adds a form on the +Post+ show page that creates a new comment by -calling the +CommentsController+ +create+ action. Let's wire that up: +calling the +CommentsController+ +create+ action. The +form_for+ call here uses +an array, which will build a nested route, such as +/posts/1/comments+. + +Let's wire up the +create+: class CommentsController < ApplicationController -- cgit v1.2.3 From 9540747747c22b8b4812abf8f8cc1abcfca9d0a2 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 20:29:52 +1000 Subject: [getting started] remove another beginning ruby link --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 8463887cfd..1bf437f411 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1427,7 +1427,7 @@ template. This is where we want the comment to show, so let's add that to the

    Text: - <%= @post.texthttp://beginningruby.org/ %> + <%= @post.text %>

    Comments

    -- cgit v1.2.3 From 39b3a10253439483b53c1665a72e0526fcf7551f Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 20:32:33 +1000 Subject: [getting started] add comma in 'First we will make a comment partial' --- guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 1bf437f411..2768d782b4 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1475,7 +1475,7 @@ use partials to clean it up. h4. Rendering Partial Collections -First we will make a comment partial to extract showing all the comments for the +First, we will make a comment partial to extract showing all the comments for the post. Create the file +app/views/comments/_comment.html.erb+ and put the following into it: -- cgit v1.2.3 From a3a25e7e447a26422db3962ba44b809f809c7729 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 17 May 2012 20:32:44 +1000 Subject: [getting started] remove two more beginningruby links --- guides/source/getting_started.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source/getting_started.textile') diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile index 2768d782b4..19bd106ff0 100644 --- a/guides/source/getting_started.textile +++ b/guides/source/getting_started.textile @@ -1502,7 +1502,7 @@ following:

    Text: - <%= @post.texthttp://beginningruby.org/ %> + <%= @post.text %>

    Comments

    @@ -1564,7 +1564,7 @@ Then you make the +app/views/posts/show.html.erb+ look like the following:

    Text: - <%= @post.texthttp://beginningruby.org/ %> + <%= @post.text %>

    Add a comment:

    -- cgit v1.2.3
    Title