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 routesto see all routes in the application to verify that the PUT /projects/:project_id/permissions/:id action exists and maps to permissions#update