Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you created user_project.rb only for storing these ids - remove it.</p> <p>This look like you need to write before_filter in your controllers</p> <pre class="lang-rb prettyprint-override"><code>class ProjectsController &lt;&lt; ApplicationController before_filter :current_user_required, only: [ :edit, :update ] #there def of actions private def current_user_required unless current_user == @project.user flash[:error] = 'error 403' redirect_to :back end end </code></pre> <p>And when you find @project, for edit and update actions, you can do it like:</p> <pre class="lang-rb prettyprint-override"><code>@project = current_user.projects.find(params[:project_id]) #need to change :project_id </code></pre> <p>If you want to create model Message(belongs_to :project and Project has_name: messages) and give access for creation and edit it only for project.user you can do it using before_filter or validation in model </p> <pre class="lang-rb prettyprint-override"><code>class Message &lt;&lt; ActiveRecord::Base validate :author_is_project_user, on: :create private def author_is_project_user errors.add :base, 'author not is project user' unless self.author == self.project.user end end </code></pre> <p>According to this you can define permission for another things</p> <p>And if you want to get permission for another user you have_to create model which belongs_ to :user and :project and check in before_filter present of it.</p> <p>sort of this:</p> <pre class="lang-rb prettyprint-override"><code>class Permission &lt;&lt; ActiveRecord::Base belongs_to :user belongs_to :project scope :about, -&gt; project { where project_id: project } scope :of_user, -&gt; user { where project_id: project } end </code></pre> <p>and in User model define method like</p> <pre class="lang-rb prettyprint-override"><code>def access_to_project? project Permission.about(project).of_user(self).first.present? end </code></pre> <p>or you can add variable to this model and make more complicated logic of access</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload