Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:如何更改模型中的布尔值?_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails:如何更改模型中的布尔值?

Ruby on rails Rails:如何更改模型中的布尔值?,ruby-on-rails,Ruby On Rails,我有一个叫“客户”的模型。客户机模型属于用户模型(用户模型与designe关联)。客户可以手动向我付款(仅限现金)。当客户付款时,我会让他们访问网站中的更多页面。为此,我想向客户机模型添加一个迁移,如下所示: rails generate migration add_paid_to_clients paid:boolean 然后,当客户付款时,我可以转到他们的个人资料,点击一个选项(可能是复选框),说客户已经付款了。只有管理员(me)才能看到此部分。我的实现方式是,在用户配置文件视图中: &l

我有一个叫“客户”的模型。客户机模型属于用户模型(用户模型与designe关联)。客户可以手动向我付款(仅限现金)。当客户付款时,我会让他们访问网站中的更多页面。为此,我想向客户机模型添加一个迁移,如下所示:

rails generate migration add_paid_to_clients paid:boolean
然后,当客户付款时,我可以转到他们的个人资料,点击一个选项(可能是复选框),说客户已经付款了。只有管理员(me)才能看到此部分。我的实现方式是,在用户配置文件视图中:

<% if current_user.user_type == :client%>
    userinformation....blablabla
<% elsif current_user.user_type == :admin %>
    userinformation....blablabla
    AND the checkbox I talked about.
<% end %>
这不是真的现金,这是学校的一个项目。

像这样的吗

在管理员的表格中:

<%= form_for @user do |f| %>
  <%= check_box_tag 'paid', 'yes', true %>
  <%= f.submit "Update" %>
<% end %>

然后确保所有受保护的页面都继承自
PaidController

别忘了保护控制器中的新属性…@BradWerth这是什么意思?除了上面概述的步骤外,您还希望在控制器中设置保护。如果有人猜到一堆属性(“付费”、“管理员”等)并使用它们提交恶意请求,您可能需要创建一个父控制器来检查用户是否付费。并显示404或403,如果他们试图访问付费页面而没有paying@BradWerth哦,好的。我将对此进行研究,你知道如何实现复选框部分tho吗?你能给我解释一下这一行吗-->我还用我的实际代码更新了我的答案。原来的问题简化了,以便更容易理解。如果你的答案因为我的改变而改变,请告诉我。你是amazing@LizzyTheLearner复选框标记创建一个复选框。参数包括:名称、选中时的值以及默认选中时的值。您可以将最后一个设置为falseOh ok。因此,在我的情况下,默认情况下,它不应该被选中。这意味着,默认情况下,客户尚未付款。所以我就到此为止。当我点击它时,表明客户已付款,当前用户已付款?会回来吗?谢谢你帮我Ruslan
class PaidController < ApplicationController
    before_filter :authorize_paid

    def authorize_paid
      return head(:forbidden) unless current_user.paid?
    end
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
class PaidController < ApplicationController
    before_filter :authorize_paid

    def authorize_paid
      return head(:forbidden) unless current_user.paid?
    end
end