params ストロングパラメーター MVC

paramsとは

ページをまたいでデータを活用したいときに活用される


```ruby
    26: def create
 => 27:   binding.pry
    28:   @task = Task.new(task_params.merge(user_id: current_user.id))
    29:
    30:   if params[:back].present?
    31:     render :new
    32:     return
    33:   end
    34:   if @task.save
    35:     TaskMailer.creation_email(@task).deliver_now
    36:     SampleJob.perform_later
    37:     flash[:notice] = "タスク#{@task.name}を登録しました"
    38:     redirect_to tasks_url
    39:   else
    40:     render :new
    41:   end
    42:
    43: end
```


```ruby

[1] pry(#<TasksController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", 
"authenticity_token"=>"/2PLg8cBtE+lsJQJCOsOZ9dP0WixA==", 
"task"=>
{"name"=>"dsa", 
"description"=>"sa"}, 
"commit"=>"登録する", 
"controller"=>"tasks", 
"action"=>"create"} 
permitted: false>

# paramsの中身を取得
[2] pry(#<TasksController>)> params[:task]
=> <ActionController::Parameters {"name"=>"dsa", "description"=>"sa"} permitted: false>
[3] pry(#<TasksController>)> params[:task][:name]
=> "dsa"
```

## form

- formの役割

![スクリーンショット 2019-11-09 13.29.54.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/bbee90de-0462-0d5c-ac0f-4266f85d4e2b.png)

- rake routes

```ruby:routes.rb
                    tasks GET    /tasks(.:format)                                                                         tasks#index
                          POST   /tasks(.:format)                                                                         tasks#create
                 new_task GET    /tasks/new(.:format)                                                                     tasks#new
                edit_task GET    /tasks/:id/edit(.:format)                                                                tasks#edit
                     task GET    /tasks/:id(.:format)                                                                     tasks#show
                          PATCH  /tasks/:id(.:format)                                                                     tasks#update
                          PUT    /tasks/:id(.:format)                                                                     tasks#update
                          DELETE /tasks/:id(.:format)                                                                     tasks#destroy
```


## MVC（ステップ）

- urlにアクセス（リクエスト）

![スクリーンショット 2019-11-09 13.32.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/d80ea42a-5ab0-60b0-0a60-259dc8781024.png)

- ルーティング

```ruby:routes.rb
                    tasks GET    /tasks(.:format)                                                                         tasks#index
                          POST   /tasks(.:format)                                                                         tasks#create
                 new_task GET    /tasks/new(.:format)                                                                     tasks#new
                edit_task GET    /tasks/:id/edit(.:format)                                                                tasks#edit
                     task GET    /tasks/:id(.:format)                                                                     tasks#show
                          PATCH  /tasks/:id(.:format)                                                                     tasks#update
                          PUT    /tasks/:id(.:format)                                                                     tasks#update
                          DELETE /tasks/:id(.:format)                                                                     tasks#destroy
```

- controller 

![スクリーンショット 2019-11-09 13.34.42.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/16da6df8-4641-485b-a98c-cccac4db898e.png)


![スクリーンショット 2019-11-09 13.35.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/b3a2bf78-eef2-b768-6f8f-4a9428437f63.png)

- view 


![スクリーンショット 2019-11-09 13.36.25.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/82aa9a58-ae50-e30f-edf7-a9f24666a510.png)

- model

![スクリーンショット 2019-11-09 13.36.48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/286971/7eeeb518-d948-7a3a-cd5c-da4c746bd0a4.png)