12
13

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 3 years have passed since last update.

Hamlでのlink toの書き方【Rails】

Posted at

Hamlにおける link to の書き方がいつも迷ってしまうのでメモ。

Hamlでのlink to の書き方

/profiles のページに飛ばしたい場合
以下どちらかの書き方がある。

sample.html.haml
#以下のどちらでもよい
= link_to "/profiles" do #①
= link_to profiles_path do #②

/profiles/new のページに飛ばしたい場合
以下どちらかの書き方がある。

sample.html.haml
#以下のどちらでもよい
= link_to "/profiles/new" do #①
= link_to new_profile_path do #②

②の書き方のpathはどこから取得するのか

ターミナルで $rails routes で出てくる。

例)この Prefix の部分

terminal
$ rails routes
                              Prefix Verb   URI Pattern                                                                              Controller#Action
                    new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
                        user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
                destroy_user_session DELETE /users/sign_out(.:format)                                                                devise/sessions#destroy
                   new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                  edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                       user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                     PUT    /users/password(.:format)                                                                devise/passwords#update
                                     POST   /users/password(.:format)                                                                devise/passwords#create
            cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
               new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
              edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
                   user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                                     PUT    /users(.:format)                                                                         devise/registrations#update
                                     DELETE /users(.:format)                                                                         devise/registrations#destroy
                                     POST   /users(.:format)                                                                         devise/registrations#create
                                root GET    /                                                                                        home#index
                             mypages GET    /mypages(.:format)                                                                       mypages#index
                               items POST   /items(.:format)                                                                         items#create
                            new_item GET    /items/new(.:format)                                                                     items#new
                                item GET    /items/:id(.:format)                                                                     items#show
                            profiles GET    /profiles(.:format)                                                                      profiles#index
                         new_profile GET    /profiles/new(.:format)                                                                  profiles#new
                        edit_profile GET    /profiles/:id/edit(.:format)                                                             profiles#edit
                                     GET    /items(.:format)                                                                         items#index
                                     POST   /items(.:format)                                                                         items#create
                                     GET    /items/new(.:format)                                                                     items#new
                                     GET    /items/:id(.:format)                                                                     items#show
12
13
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
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?