我有一个超级简单的问题。我有一个页面列出了我的应用程序中的所有产品。我只想让该页面只能由管理员查看。但是产品/新品我希望每个人都能看清楚。
架构.rb
create_table "users", :force => true do |t|
t.string "email"
t.string "password_hash"
t.string "password_salt"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.boolean "admin", :default => false
end
产品控制器
class ProductsController < ApplicationController
before_filter :require_login
before_filter :current_user, only: [:create, :destory]
before_filter :correct_user, only: :destory
def index
@products = Product.all
end
def new
@product = Product.new
end
def create
@product = current_user.products.new(params[:product])
if @product.valid?
@product.save
render "show", :notice => "Sale created!"
else
render "new", :notice => "Somehting went wrong!"
end
end