2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

パラメータの受け渡し、およびパスについて

Last updated at Posted at 2019-04-04

Railsではパラメータの受け渡しが非常に重要です。
詰まると大変です。

色々とルールがあります。
ストロングパラメータや他モデルのカラムデータを使う場合など。

今回は、その中でも、最近少し気になったパスの書き方について、見て行きたいと思います。

パス

まずは、以下のルーティングをご覧ください。

                   Prefix Verb   URI Pattern                                                                            
                                          Controller#Action
              admin_users GET    /admin/users(.:format)                                                                   
                                          admin/users#index
                          POST   /admin/users(.:format)                                                                   
                                          admin/users#create
           new_admin_user GET    /admin/users/new(.:format)                                                               
                                          admin/users#new
          edit_admin_user GET    /admin/users/:id/edit(.:format)                                                          
                                          admin/users#edit
               admin_user GET    /admin/users/:id(.:format)                                                               
                                          admin/users#show
                          PATCH  /admin/users/:id(.:format)                                                               
                                          admin/users#update
                          PUT    /admin/users/:id(.:format)                                                               
                                          admin/users#update
                          DELETE /admin/users/:id(.:format)                                                               
                                          admin/users#destroy

これは、routes.rbファイルにて、resourcesを使用したルーティングです。
前回の記事から転用しているので、admin名前空間付きですが。

そして、これらのパスを使うのは、パラメータの受け渡しです。
大抵、

  • link_to
  • redirect_to
  • render
  • form_with

の4つを駆使しますね。

もちろん、第二引数やネスト、method型などもありますが、パスの形としては、絶対パスなのか、相対パスなのかというところが基本ですね。

今回は気になったパスの書き方をお伝えして行きます。

パスの書き方

  • [:admin, @user]→多分、これはAdminのusers_controllerに飛んでる。Railsがよしなにしてくれる感じ。データなければcreate、あればshowとかupdate、場合によってはdestroyに飛ぶ感じですね。前に「model: 」と記載されることも。
  • admin_users_url→これはヘルパーなのですが、urlの前の部分がルーティングで表示されてました。基本、「:id」(params[:id])受け渡しの有無に関係なく使えます。対応のcontrollerメソッドに飛びます。
  • admin_user_path(@user)→これもヘルパーです。ただし、こちらは、「:id」(params[:id])受け渡しの際に積極的に使われる模様。@userは@user.idの省略形です。対応のcontrollerメソッドに飛びます。

パスは奥が深いですが、ここを間違えると、エラーの嵐ですので、頑張りましょう。

renderのデータ受け取り

viewを作る際に、フォームは共通の型にしてそれぞれのファイルで読み込むことが多いです。
共通の型を大抵、「_form.html.rb」と名付けます。

そして各ファイルにて以下のように、読み込みます。

<%=render partial: 'form', locals: (user: @user) %>

これはなんというか、定型文だと思っているので、そういうものだということにしています。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?