Skip to content

Commit

Permalink
Merge pull request #11562 from zzak/backport_efeb039
Browse files Browse the repository at this point in the history
backport efeb039 from #11201 fixes #11540 [ci skip]
  • Loading branch information
rafaelfranca committed Jul 22, 2013
2 parents bddb73e + 5ddb94d commit 883598b
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions guides/source/getting_started.md
Expand Up @@ -531,29 +531,20 @@ and change the `create` action to look like this:


```ruby ```ruby
def create def create
@post = Post.new(post_params) @post = Post.new(params[:post])
@post.save @post.save
redirect_to @post redirect_to @post
end end

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


Here's what's going on: every Rails model can be initialized with its Here's what's going on: every Rails model can be initialized with its
respective attributes, which are automatically mapped to the respective respective attributes, which are automatically mapped to the respective
database columns. In the first line we do just that (remember that database columns. In the first line we do just that (remember that
`post_params` contains the attributes we're interested in). Then, `params[:post]` contains the attributes we're interested in). Then,
`@post.save` is responsible for saving the model in the database. `@post.save` is responsible for saving the model in the database.
Finally, we redirect the user to the `show` action, Finally, we redirect the user to the `show` action,
which we'll define later. which we'll define later.


TIP: Note that `def post_params` is private. This new approach prevents an
attacker from setting the model's attributes by manipulating the hash passed
to the model. For more information, refer to [this blog post about Strong Parameters](http://weblog.rubyonrails.org/2012/3/21/strong-parameters/).

TIP: As we'll see later, `@post.save` returns a boolean indicating TIP: As we'll see later, `@post.save` returns a boolean indicating
whether the model was saved or not. whether the model was saved or not.


Expand Down Expand Up @@ -627,6 +618,10 @@ Visit <http://localhost:3000/posts/new> and give it a try!


![Show action for posts](images/getting_started/show_action_for_posts.png) ![Show action for posts](images/getting_started/show_action_for_posts.png)


TIP: Note that `def post_params` is private. This new approach prevents an
attacker from setting the model's attributes by manipulating the hash passed
to the model. For more information, refer to [this blog post about Strong Parameters](http://weblog.rubyonrails.org/2012/3/21/strong-parameters/).

### Listing all posts ### Listing all posts


We still need a way to list all our posts, so let's do that. We still need a way to list all our posts, so let's do that.
Expand Down

0 comments on commit 883598b

Please sign in to comment.