我们说我有一个' 会员'和一个' 项目'模型。
'的会员'和' 模型'反映多个到多个关联,并通过project_members连接表进行链接。
任何成员都可以扮演与她相关的所有项目中可能不一样的角色。
我试图写一个' add_role '和' remove_role '将角色分配给成员的方法。
由于角色应该按照项目/成员分配,我已经构建了一个控制器&project'members_controller'我可以写那些方法。
我感到困惑的是,考虑到我为这些方法定义路由的方式,我得到两条路径相同的POST路由。因此,听起来我做错了什么或者不了解Rails如何运作。
目前我的路线定义为:
resources :projects do
member do
get "collaborators", to: "members/members#index"
post "collaborators/:id/set_role", to: "project_members#set_role"
post "collaborators/:id/remove_role", to: "project_members#remove_role"
end
end
在以下路线中翻译:
Prefix Verb URI Pattern Controller#Action
collaborators_project GET /projects/:id/collaborators(.:format) members/members#index
POST /projects/:id/collaborators/:id/set_role(.:format) project_members#set_role
POST /projects/:id/collaborators/:id/remove_role(.:format) project_members#remove_role
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
URI模式是我瞄准的目标,但这听起来像是一种好习惯吗?
如果add_role和remove_role方法共享相同的路径,我如何通过link_to访问它们。我应该在助手中填一个网址吗?和......
在做这样的事情时使用POST是否正确或者我应该使用PATCH?
我很感激你的指导。
作为参考我的' project_members_controller.rb'包含类似的内容:
def set_role
@project = params[:project_id]
if @project.owner.id == current_member.id
@project_member = ProjectMember.where(project_id: @project.id, member_id: params[:id])
@project_member.add_role(:observer) if self.roles.blank?
# CODE RENDERING OUTPUT
render html: "<h1>HOLA</h1>"
else
# CODE THROWIGN AN ERROR
end
end
答案 0 :(得分:2)
始终使用RESTful Rails路由
您可以将路线更改为以下
更新1:
resources :projects do
member do
get "collaborators", to: "members/members#index"
end
resources :collaborators, only: [:create, :destroy]
end
更新2:
resources :projects do
member do
get "collaborators", to: "members/members#index"
end
resources :collaborators, only: [] do
member do
post 'set_role', => 'project_members#set_role'
post 'remove_role' => "project_members#remove_role"
end
end
end
更新3:
resources :projects do
resources :collaborators, only: [] do
get '/' => "members/members#index", on: :collection
member do
post 'set_role' => 'project_members#set_role'
post 'remove_role' => "project_members#remove_role"
end
end
end
答案 1 :(得分:0)
您可以编写一个公共方法 <select class="form-control" name="cityID" id="Select1" ng-change="RoomSelect()" ng-model="sRoom">
<option value="1" selected="selected">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
来添加/删除角色,而不是使用单独的方法添加和删除角色。
修改路线文件。
manage_roles
您可以通过点击链接传递参数,您可以根据该参数来添加或删除角色。
resources :projects do
member do
get "collaborators", to: "members/members#index"
post "collaborators/:id/manage_roles", to: "project_members#manage_roles"
end
end
在您的控制器中,添加<%= link_to "Add Role", <manage_roles path> , {role_action: 'add_role', method: :post} %>
方法
manage_roles