Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For users to be able to read the tickets they've created, you just need to add a condition on the ability (see below). You can use the same condition on the <code>:create</code> ability and cancan will pre-fill those attributes for you when it builds a new object for the <code>#new</code> or <code>#create</code> actions.</p> <pre><code># app/models/ticket.rb class Ticket &lt; ActiveRecord::Base # &lt;snip&gt; belongs_to :user belongs_to :employee # &lt;snip&gt; end # app/models/user.rb class User &lt; ActiveRecord::Base has_one :employee end # app/models/ability.rb class Ability # &lt;snip&gt; if user.has_role? :ticket_viewer can :create, Ticket can :read, Ticket, :user_id =&gt; user.id end if user.employee # &amp;&amp; any other necessary conditions can :create, Ticket can :read, Ticket, :employee_id =&gt; user.employee.id end end # app/controllers/tickets_controller.rb controller TicketsController &lt; ApplicationController load_and_authorize_resource def index # @tickets = Ticket.accessible_by(current_ability) # cancan's # load_and_authorize resource will take care of loading ticket(s) for # all controller actions, so I've commented them out @tickets_grid = initialize_grid(@tickets, :include =&gt; [{:user =&gt; :user_type}, :employee_department, :state]) end def show # @ticket = Ticket.find(params[:id]) @reply = @ticket.replies.build # this for comments on ticket @state = State.all # this for a model called State which describe the priority of the ticket (Emergency / High / Normal ) end def new # @ticket = Ticket.new end def create # @ticket = Ticket.new(params[:ticket]) if @ticket.save flash[:notice] = 'Support ticket request created.' redirect_to @ticket else flash[:error] = 'An error occurred please try again!' redirect_to '/dashboard' end end def edit # @ticket = Ticket.find(params[:id]) end def update # @ticket = Ticket.find(params[:id]) if @ticket.update_attributes(params[:ticket]) flash[:notice] = 'Successfuly updated.' redirect_to tickets_path else flash[:error] = 'An error occurred please try again!' render @ticket end end end </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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