Restful 使用带有复选框选项的表单路由嵌套资源
Restful Routing for Nested Resource with a Form with Checkbox Options
我的模型是:
class User < ApplicationRecord
has_many :user_stores
has_many :stores, through: :user_stores
end
class UserStore < ApplicationRecord
belongs_to :user
belongs_to :store
end
class Store < ApplicationRecord
has_many :user_stores
has_many :users, through: :user_stores
end
我的路线:
Rails.application.routes.draw do
devise_for :users
root 'home#index'
resources :user do
resources :stores
end
end
rails 航线:
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
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / home#index
user_stores GET /user/:user_id/stores(.:format) stores#index
POST /user/:user_id/stores(.:format) stores#create
new_user_store GET /user/:user_id/stores/new(.:format) stores#new
edit_user_store GET /user/:user_id/stores/:id/edit(.:format) stores#edit
user_store GET /user/:user_id/stores/:id(.:format) stores#show
PATCH /user/:user_id/stores/:id(.:format) stores#update
PUT /user/:user_id/stores/:id(.:format) stores#update
DELETE /user/:user_id/stores/:id(.:format) stores#destroy
user_index GET /user(.:format) user#index
POST /user(.:format) user#create
new_user GET /user/new(.:format) user#new
edit_user GET /user/:id/edit(.:format) user#edit
user GET /user/:id(.:format) user#show
PATCH /user/:id(.:format) user#update
PUT /user/:id(.:format) user#update
DELETE /user/:id(.:format) user#destroy
在我的加入table中,我要记录用户最喜欢的店铺,用户想收藏的店铺。这将通过带有复选框的表单来完成,并且用户已经喜欢的商店将被选中。
我有两条路让我很困惑。当用户单击按钮转到页面以查看其最喜欢的商店时,它应该转到:
1./user/:user_id/stores(.:format)
(如果我走这条路线,当提交表单时,它应该 Post 到路径 /user/:user_id/stores
,然后在控制器中我将采用参数 store_ids[]
并遍历每个商店 ID,将其创建到数据库。)
还是应该去
2./user/:id
(如果我转到我的表单当前执行的这条路线,我将不得不使用 Patch 到路径 /user/:id
。在这一点上,因为我正在使用 Devise,我将不得不创建一个用户的控制器,并使用 store_ids
参数更新用户,同时循环 store_ids
以创建连接 table 条目)
我知道这可能是一个 multi-part 问题,但无论哪条路线更合适,我将如何布置表格以反映这一点?目前我有:
<%= simple_form_for(@user, html: { class: 'form-horizontal' }) do |f| %>
<%= f.association :stores, as: :check_boxes, label: false %>
<%= f.button :submit, "Update Favorite Stores", class: "btn btn-primary" %>
<% end %>
但这会转到用户控制器。
第一个合适。第二个是用户展示页面。
在您的表单助手中,您可以只使用集合 select(我不确定我是否使用了以下正确的语法):
<%= f.collection_select :user, :store_ids, Store.all.order(name: :asc), :id, :name, {}, { multiple: true, class: "form-control" } %>
我的模型是:
class User < ApplicationRecord
has_many :user_stores
has_many :stores, through: :user_stores
end
class UserStore < ApplicationRecord
belongs_to :user
belongs_to :store
end
class Store < ApplicationRecord
has_many :user_stores
has_many :users, through: :user_stores
end
我的路线:
Rails.application.routes.draw do
devise_for :users
root 'home#index'
resources :user do
resources :stores
end
end
rails 航线:
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
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / home#index
user_stores GET /user/:user_id/stores(.:format) stores#index
POST /user/:user_id/stores(.:format) stores#create
new_user_store GET /user/:user_id/stores/new(.:format) stores#new
edit_user_store GET /user/:user_id/stores/:id/edit(.:format) stores#edit
user_store GET /user/:user_id/stores/:id(.:format) stores#show
PATCH /user/:user_id/stores/:id(.:format) stores#update
PUT /user/:user_id/stores/:id(.:format) stores#update
DELETE /user/:user_id/stores/:id(.:format) stores#destroy
user_index GET /user(.:format) user#index
POST /user(.:format) user#create
new_user GET /user/new(.:format) user#new
edit_user GET /user/:id/edit(.:format) user#edit
user GET /user/:id(.:format) user#show
PATCH /user/:id(.:format) user#update
PUT /user/:id(.:format) user#update
DELETE /user/:id(.:format) user#destroy
在我的加入table中,我要记录用户最喜欢的店铺,用户想收藏的店铺。这将通过带有复选框的表单来完成,并且用户已经喜欢的商店将被选中。
我有两条路让我很困惑。当用户单击按钮转到页面以查看其最喜欢的商店时,它应该转到:
1./user/:user_id/stores(.:format)
(如果我走这条路线,当提交表单时,它应该 Post 到路径 /user/:user_id/stores
,然后在控制器中我将采用参数 store_ids[]
并遍历每个商店 ID,将其创建到数据库。)
还是应该去
2./user/:id
(如果我转到我的表单当前执行的这条路线,我将不得不使用 Patch 到路径 /user/:id
。在这一点上,因为我正在使用 Devise,我将不得不创建一个用户的控制器,并使用 store_ids
参数更新用户,同时循环 store_ids
以创建连接 table 条目)
我知道这可能是一个 multi-part 问题,但无论哪条路线更合适,我将如何布置表格以反映这一点?目前我有:
<%= simple_form_for(@user, html: { class: 'form-horizontal' }) do |f| %>
<%= f.association :stores, as: :check_boxes, label: false %>
<%= f.button :submit, "Update Favorite Stores", class: "btn btn-primary" %>
<% end %>
但这会转到用户控制器。
第一个合适。第二个是用户展示页面。
在您的表单助手中,您可以只使用集合 select(我不确定我是否使用了以下正确的语法):
<%= f.collection_select :user, :store_ids, Store.all.order(name: :asc), :id, :name, {}, { multiple: true, class: "form-control" } %>