Skip to content

Commit

Permalink
Merge pull request #11186 from jetthoughts/synchronize_create_method_…
Browse files Browse the repository at this point in the history
…body

Synchronize PostController#create code from 5.6 to others sections in Getting Started guide.
  • Loading branch information
senny committed Jun 30, 2013
2 parents 162cc66 + bb8f0b4 commit 996f9b5
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions guides/source/getting_started.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -612,11 +612,16 @@ we want to accept in our controllers. In this case, we want to allow the
look like this: look like this:


``` ```
def create def create
@post = Post.new(params[:post].permit(:title, :text)) @post = Post.new(post_params)
@post.save @post.save
redirect_to @post redirect_to @post
end
private
def post_params
params.require(:post).permit(:title, :text)
end end
``` ```


Expand Down Expand Up @@ -767,14 +772,19 @@ def new
end end


def create def create
@post = Post.new(params[:post].permit(:title, :text)) @post = Post.new(post_params)


if @post.save if @post.save
redirect_to @post redirect_to @post
else else
render 'new' render 'new'
end end
end end

private
def post_params
params.require(:post).permit(:title, :text)
end
``` ```


The `new` action is now creating a new instance variable called `@post`, and The `new` action is now creating a new instance variable called `@post`, and
Expand Down Expand Up @@ -905,19 +915,26 @@ Next we need to create the `update` action in `app/controllers/posts_controller.
def update def update
@post = Post.find(params[:id]) @post = Post.find(params[:id])


if @post.update(params[:post].permit(:title, :text)) if @post.update(post_params)
redirect_to @post redirect_to @post
else else
render 'edit' render 'edit'
end end
end end

private
def post_params
params.require(:post).permit(:title, :text)
end
``` ```


The new method, `update`, is used when you want to update a record The new method, `update`, is used when you want to update a record
that already exists, and it accepts a hash containing the attributes that already exists, and it accepts a hash containing the attributes
that you want to update. As before, if there was an error updating the 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. post we want to show the form back to the user.


We reuse the `post_params` method that we defined earlier for the create action.

TIP: You don't need to pass all attributes to `update`. For TIP: You don't need to pass all attributes to `update`. For
example, if you'd call `@post.update(title: 'A new title')` example, if you'd call `@post.update(title: 'A new title')`
Rails would only update the `title` attribute, leaving all other Rails would only update the `title` attribute, leaving all other
Expand Down Expand Up @@ -1303,9 +1320,14 @@ Let's wire up the `create` in `app/controllers/comments_controller.rb`:
class CommentsController < ApplicationController class CommentsController < ApplicationController
def create def create
@post = Post.find(params[:post_id]) @post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body)) @comment = @post.comments.create(comment_params)
redirect_to post_path(@post) redirect_to post_path(@post)
end end

private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end end
``` ```


Expand Down Expand Up @@ -1527,10 +1549,9 @@ controller (`app/controllers/comments_controller.rb`):


```ruby ```ruby
class CommentsController < ApplicationController class CommentsController < ApplicationController

def create def create
@post = Post.find(params[:post_id]) @post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment]) @comment = @post.comments.create(comment_params)
redirect_to post_path(@post) redirect_to post_path(@post)
end end


Expand All @@ -1541,6 +1562,10 @@ class CommentsController < ApplicationController
redirect_to post_path(@post) redirect_to post_path(@post)
end end


private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end end
``` ```


Expand Down

0 comments on commit 996f9b5

Please sign in to comment.