浅いネストのルーティング設定
ネストのルーティング設定をするときに、shallowオプションが便利です。
resources :organizations do resources :users, shallow: true end
そうするとroutesはこんな感じに設定されます。
organization_users GET /organizations/:organization_id/users(.:format) users#index POST /organizations/:organization_id/users(.:format) users#create new_organization_user GET /organizations/:organization_id/users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy
Viewのパス設定
form_for
- /users/_form.html.erb
<%= form_for(shallow_args @organization, @user) do |f| %>
- application_helper.rb
def shallow_args(parent, child) child.try(:new_record?) ? [parent, child] : child end
users#indexへのリンク
<%= link_to 'Back', organization_users_path(@organization) %>