0
1

More than 3 years have passed since last update.

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

Posted at

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の役割

スクリーンショット 2019-11-09 13.29.54.png

  • 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にアクセス(リクエスト)

スクリーンショット 2019-11-09 13.32.28.png

  • ルーティング
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

スクリーンショット 2019-11-09 13.35.29.png

  • view

スクリーンショット 2019-11-09 13.36.25.png

  • model

スクリーンショット 2019-11-09 13.36.48.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1