paramsとは
ページをまたいでデータを活用したいときに活用される
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
[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の役割
- rake routes
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にアクセス(リクエスト)
- ルーティング
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
- view
- model