我有一个 Ruby on Rails 应用程序来处理用户收入,决定它是津贴还是收入并应用适当的税率。我已经包含了一些enums执行基本功能的内容,如下所述。
当我从默认值开始更新时,我遇到了问题“1”不是有效的收入类型。
您可以在下面看到设置,包括模型、控制器和表单。
模型 :
class Income < ApplicationRecord
enum incomeType: {income: 0, allowance: 1 }
enum taxed: {yes: 0, no: 1 }
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
控制器:
class IncomesController < ApplicationController
before_action :set_income, only: [:show, :edit, :update, :destroy]
# GET /incomes
# GET /incomes.json
def index
@incomes = current_user.incomes.all
end
# GET /incomes/1
# GET /incomes/1.json
def show
end
# GET /incomes/new
def new
@income = current_user.incomes.build
end
# GET /incomes/1/edit
def edit
end …Run Code Online (Sandbox Code Playgroud)