我有一个名为" Clients"的模型。客户端模型属于用户模型(用户模型与设计相关联)。客户可以手动向我付款(仅限现金)。当客户付款时,我会让他们访问网站上的更多页面。为此,我想我将迁移添加到客户端模型,如:
let levelId = 2
// Module Name = Product Name from your Build Settings, unless you put
// the Level classes into sub-namespace
let className = "ModuleName.Level\(levelId)"
let LevelClass = NSClassFromString(className) as! Level.Type
// At build time, the compiler sees this as an object of type Level,
// but it's actually a Level2 object. You can cast it to Level2 if
// you need to do something specific to that level.
let level = LevelClass.init(size: frame.size)
然后,当客户付钱时,我可以转到他们的个人资料,然后点击一个选项(可能是一个复选框),说客户已付款。只有管理员(我)可以看到这部分。我将实现的方式是,在用户个人资料视图中:
rails generate migration add_paid_to_clients paid:boolean
user_type是一个预定义的函数,用于确定current_user是admin还是客户端。我已经完成了这一部分。
所以在私人网页(支付后可以访问的页面)中,我可以有一些逻辑,如<% if current_user.user_type == :client%>
userinformation....blablabla
<% elsif current_user.user_type == :admin %>
userinformation....blablabla
AND the checkbox I talked about.
<% end %>
如何实施复选框部分?迁移后(如果有更好的方法,请让我知道),我怎样才能只是&#34;翻转开关&#34;让他们获得更多的访问权限?
我的客户控制器:
If current_user.paid? THEN show them this page.
这不是真正的现金,这是一个学校项目。
答案 0 :(得分:0)
这样的东西?
在admin的表单中:
<%= form_for @user do |f| %>
<%= check_box_tag 'paid', 'yes', true %>
<%= f.submit "Update" %>
<% end %>
然后在控制器中:
class ClientController < ApplicationController
before_action :find_client, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@client = current_user.build_client
end
def create
@client = current_user.build_client(client_params)
if @client.save
redirect_to clients_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
@client.destroy
redirect_to root_path
end
private
def client_params
if current_user.user_type == :admin
# This is different because only admin can update paid
# To make a string a boolean
params[:client][:paid] = params[:client][:paid] == 'yes'
params.require(:client).permit(:paid, :name, :company, :position, :number, :email, :client_img)
else
params.require(:client).permit(:name, :company, :position, :number, :email, :client_img)
end
end
def find_client
@client = Client.find(params[:id])
end
end
您可以使用受保护的属性described here
更正确地执行此操作之后你想制作一个PaidController
class PaidController < ApplicationController
before_filter :authorize_paid
def authorize_paid
return head(:forbidden) unless current_user.paid?
end
end
然后确保所有受保护的页面都继承自PaidController