I’m working with three models: user, project, and userprivilege. The userprivilege model consists of a user_id, a project_id, and an accessLevel value. The user and project models have a ‘has_many’ relationship with userprivilege, and a ‘has_many, through’ relationship with each other.
When a user creates a project, a corresponding entry is created in the userprivileges table. I’m not sure how to create such an entry for a situation where a user shares a project with another user. In this situation, a user visits a project page, clicks on a link to share access to the project, and specifies the e-mail of the target user.
I’m associating this situation with the ‘new’ action in the UserprivilegesController. In the ‘create’ action, I can access the user object with whom the project is being shared (using the email id), but I’m not sure how to get the project id of the project being shared. Is there a way to store the project id when the user visits the project page and clicks on the share access link?
Thanks.
I was able to solve this issue thanks to Aldo’s comment. But, I have a related question: Is it possible to approach this using the routes.rb file. Could I include the routes for the Userprivilege actions in the Project routes? As examples, something like: /projects/1/userprivileges/new or /projects/1/userprivileges/1/edit. Then, I could get the project id from the URI. But, a restriction would be necessary such that userprivileges can only be accessed from the projects to which they relate, so something like /projects/2/userprivileges/1/edit wouldn’t be allowed. How could I implement that restriction?
You should pass the
project_idtoUserprivilegesController#new. Use the link_to options to do this in your project view, for example:Then once in that controller you get this value from the parameters using
params[:project_id]and add somewhere in your form using an hidden field (usinghidden_fieldorhidden_field_tagdepending on the way your’re building your form).The idea is to pass the
project_idwhen the user submit the form so that theUserprivilegesController#createaction can access it using (againparams[:project_id]).