rails s 启动项目时候默认访问地址是 localhost:3000 例如:本机 ip 为 192.168.8.8 我现在执行:rails s -b 192.168.8.8 现在可以通过:192.168.8.8:3000 指定了通过本机 ip 访问 可是报了如下错误: 不知道这是 cancan 默认设置的问题还是哪出问题了?报了一个没有权限的 error[第一次用 cancan 求大神指点!!!] ability.rb 代码如下:
class Ability
include CanCan::Ability
def initialize(user)
if user.nil?
cannot :manage, :all
elsif user.genre == 1 or user.genre == 2 or user.genre == 3
can :manage, :all
else
cannot :manage, :all
end
end
end
users_controller.rb 如下:
# coding: utf-8
class Admin::UsersController < Admin::BaseController
before_action :find_user, only: [:edit, :update, :password, :change_password, :destroy]
def index
end
def new
@user = User.new
end
def edit
end
def create
@user = User.new(user_params)
if @user.save
redirect_to admin_users_url, notice: '新建成功!'
else
render :new
end
end
def update
get_user_params = user_params
get_user_params.except!(:password, :password_confirmation) if user_params[:password].blank? && user_params[:password_confirmation].blank?
if @user.update(get_user_params)
redirect_to admin_users_url, notice: '更新成功!'
else
render :edit
end
end
def password
render json: @user
end
def change_password
if @user.update(user_params)
redirect_to "/admin/users"
else
redirect_to "/admin/users"
end
end
def destroy
@user.state? ? @user.update(state: false) : @user.update(state: true)
redirect_to admin_users_url
end
private
def find_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :remember_me, :username, :user_pic , :genre, :description, :phone, :phone_verify, :private_token, :weibo_uid, :qq_uid, :wechat_uid, :twitter_uid, :message_count ,:background_image_pic , :source , :_alias , :comments_push_switch , :praises_push_switch , :letter_push_switch , :letter_count, :address, :status)
end
end
base_controller.rb 如下:
# coding: utf-8
class Admin::BaseController < ApplicationController
layout 'admin'
before_filter :authenticate_user!
authorize_resource
private
def current_ability
@current_ability ||= AdminAbility.new(current_user)
end
end