Form_for 嵌套资源权限(Y 模型) [英] Form_for a Nested Resources Permissions (Y Model)

查看:27
本文介绍了Form_for 嵌套资源权限(Y 模型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的可以使用你的帮助,理解这个复杂的导轨问题我一直在撞墙......

我有以下模型:

权限(user_id、role_id、project_id)角色(ID,名称)项目(ID,名称)

权限是角色和项目的 Y 模型.

我正在努力制作一个表单以允许用户添加或更新用户的项目权限

在控制器中我有:

@project = Space.find(params[:project_id])@permission = @project.permissions.find_by_user_id(params[:user_id])

然后在视图中:

<%=form_for [:space, @permission] do |f|%>....

但这不起作用,错误:

  • 如果(用户和项目)的标准在数据库中没有任何记录:未定义方法`model_name' for NilClass:Class"

  • 如果数据库中有记录,则表示该用户确实在该项目中具有角色:"No route matching {:action=>"destroy", :controller=>"permissions", :project_id=>#<权限project_id: 3, role_id: 2, user_id: 13>}"

理想情况下,我希望此表单显示给定(已使用/项目)的当前权限(如果有)...如果没有当前权限,我希望此人能够创建记录.

有人在 Rails 3 中遇到过这种关系吗?感谢您向我发送任何提示.

路由文件(特定于这些模型的部分)

资源:项目做资源:照片,:权限收藏做获取您当前的项目"结尾结尾

控制器- 用户为 CRUD 提供权限的路径:/projects/3/permissions- 我相信我想使用 Permissions.rb def Show 和 def Update 来获取和设置权限?

解决方案

对于第一个错误,你有一个 nil 对象,你应该在你将对象传递给表单的任何地方传递一个空白对象,例如 @permission ||= @project.permissions.new(假设它是导致 nil 对象的权限对象.或者更高级的 @project.permissions.find_or_initialize_by_user_id(params[:user_id]).

对于第二个错误,奇怪的是它试图映射到销毁,它应该尝试映射到 :update 操作,对吗?运行 rake routes 以查看应用程序中的所有路由,以验证 PUT/projects/:project_id/permissions/:id 操作是否存在并映射到权限#update

could really use your help, understanding this complex rails issue I've been banging by head against the wall on...

I have the following Models:

Permissions (user_id, role_id, project_id) 
Roles (id, name) 
Projects (id, name)

Permissions is the Y model for Roles and Projects.

I'm struggling to make a form to allow a user to either Add or Update a User's Project permission

In the controller I have:

@project = Space.find(params[:project_id])
@permission = @project.permissions.find_by_user_id(params[:user_id])

And then in the view:

<%=form_for [:space, @permission] do |f| %>....

But this isn't working, errors:

  • If the criteria of (user & project) don't have any records in the DB: "undefined method `model_name' for NilClass:Class"

  • If there is a record in the DB, meaning the user does have a role on this project: "No route matches {:action=>"destroy", :controller=>"permissions", :project_id=>#<Permission project_id: 3, role_id: 2, user_id: 13>}"

Ideally, I want this form to show the current permission if any for the given (Used/Project)... If there isn't a current permission, I want the person to be able to create a record.

Anyone experiences around this type of relationship in Rails 3? Thank you for any tips you can send my way.

Routes file (the part that's specific to these models)

resources :projects do
  resources :photos, :permissions
    collection do
        get 'yourcurrentprojects'
    end
end

Controllers - Path where users are giving the option to CRUD a permission: /projects/3/permissions - I believe I'd want to use Permissions.rb def Show and def Update to get and set permissions?

解决方案

For the first error where you have a nil object you should pass in a blank object wherever you're passing objects to forms eg @permission ||= @project.permissions.new (assuming it's the permission object that is causing the nil object. Or to be fancier @project.permissions.find_or_initialize_by_user_id(params[:user_id]).

For the second error, It's odd that it's trying to map to destroy, it should be trying to map to the :update action right? run rake routes to see all routes in the application to verify that the PUT /projects/:project_id/permissions/:id action exists and maps to permissions#update

这篇关于Form_for 嵌套资源权限(Y 模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆