kyopa’s blog

自分用プログラミングアウトプット

def new
@target = Target.new
super
end

 

 

superの記述

 

class Human
def sleep
puts "寝た"
end
end
 
class Programmer < Human
def sleep
super
puts "しかし何故かぐっすり眠れない"
end
end
 
programmer = Programmer.new
programmer.sleep
 
#実行結果
#寝た
#しかし何故かぐっすり眠れない

 

継承する役割とセーブの役割かな?

 

def create
super
target = @user.targets.build(target_params)
target.save!
end

 

userの値をセーブしてから

userとtargets.をアソシエーション後

targetをセーブ

 

 

 

rails routes grep | target

 

このコマンドでtargetのみのルーティングを見ることができる。

 

 

 

 

resources :targets doの時

rails routes grep | target

 

top_targets GET /targets/top(.:format) targets#top
targets GET /targets(.:format) targets#index
POST /targets(.:format) targets#create
new_target GET /targets/new(.:format) targets#new
edit_target GET /targets/:id/edit(.:format) targets#edit
target GET /targets/:id(.:format) targets#show
PATCH /targets/:id(.:format) targets#update
PUT /targets/:id(.:format) targets#update
DELETE /targets/:id(.:format) targets#destroy

 

resource :targets doの時

 

rails routes | grep target
top_target GET /target/top(.:format) targets#top
new_target GET /target/new(.:format) targets#new
edit_target GET /target/edit(.:format) targets#edit
target GET /target(.:format) targets#show
PATCH /target(.:format) targets#update
PUT /target(.:format) targets#update
DELETE /target(.:format) targets#destroy
POST /target(.:format) targets#create

 

 

単数にするとpathのidを必要としなくなる

値が一つしかない場合は単数にすると可読性がupする

 

 

アソシエーションしているケースだと

has_manyではなくhas_oneとかにする

 

 

rails g devise:controllers users

バイスコントローラーを作成

devise_for :users, controllers: {
registrations: 'users/registrations'
}

ルーターの設定

こうすることでデバイスコントローラ(registrations)が使える