Skip to content

Instantly share code, notes, and snippets.

View xander-miller's full-sized avatar

Xander Miller xander-miller

View GitHub Profile
require 'faker'
#create 15 topics
topics = [ ]
15.times do
topics << Topic.create(
name: Faker::Lorem.sentence,
description: Faker::Lorem.paragraph)
end
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
class WikisController < ApplicationController
before_action :set_wiki, only: [:show, :edit, :update, :destroy]
# GET /wikis
# GET /wikis.json
def index
@wikis = policy_scope(Wiki)
end
class WikiPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
def resolve
if user.admin?
scope.all
else
scope.where(user: user)
end
end
<h1>My wikis</h1>
<div class="row">
<% if policy(Wiki.new).create? %> <%#added policy instead of if user %>
<%= link_to 'New', new_wiki_path, class: 'btn btn-primary btn-large' %>
<% end %>
<% if current_user.role?("free") %>
<h2><%= link_to "Upgrade Account", new_charge_path, class: 'btn btn-primary btn-large' %></h2>
</div>
<% end %>
class WikisController < ApplicationController
before_action :set_wiki, only: [:show, :edit, :update, :destroy]
# GET /wikis
# GET /wikis.json
def index
@wikis = policy_scope(Wiki)
end
<%= form_for(@wiki) do |f| %>
<% if @wiki.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@wiki.errors.count, "error") %> prohibited this wiki from being saved:</h2>
<ul>
<% @wiki.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!