concept " New Ticket " link in category rails

appears as: “New Ticket” link
Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

To create tickets for your application, you need an idea of what you’re going to implement. You want to create tickets only for particular projects, so you need a “New Ticket” link on a project’s show page. The link must lead to a form where a name and a description for your ticket can be entered, and the form needs a button that submits it to a create action in your controller. You also want to ensure that the data entered is valid, as you did with the Project model. This new form will look like figure 5.1.

Hiding the “New Ticket” link

Only some of your users viewing a project’s details will have permission to create a new ticket, so you should hide the “New Ticket” link for those users.

Open spec/features/hidden_links_spec.rb and add some more scenarios for the “New Ticket” link:

RSpec.feature "Users can only see the appropriate links" do
  ...
  context "non-admin users (project viewers)" do
    ...

    scenario "cannot see the New Ticket link" do
      visit project_path(project)
      expect(page).not_to have_link "New Ticket"
    end
  end

  context "admin users" do
    ...

    scenario "can see the New Ticket link" do
      visit project_path(project)
      expect(page).to have_link "New Ticket"
    end
  end
  ...
end

The first spec will fail when you run bundle exec rspec spec/features/hidden_links _spec.rb because all users viewing the page can see the “New Ticket” link:

1) Users can only see the appropriate links non-admin users (project
   viewers) cannot see the New Ticket link
   Failure/Error: expect(page).not_to have_link "New Ticket"
     expected not to find link "New Ticket", found 1 match: "New Ticket"
   # ./spec/features/hidden_links_spec.rb:38:in ...

Now you can use Pundit’s policy helper to hide the link when it needs to be hidden. Open app/views/projects/show.html.erb, which is where the “New Ticket” link lives.

Listing 8.52. Tickets header on the projects show view
<header> <h2>Tickets</h2> <ul class="actions"> <li><%= link_to "New Ticket", new_project_ticket_path(@project), class: "new" %></li> </ul> </header>

You can wrap the policy method around the list item that displays the “New Ticket” link, to only show the link if the user has permission to create the ticket.