Ruby on rails 我需要更新rails中的属性吗?(怎么做?)
我为一个类生成了脚手架:Ruby on rails 我需要更新rails中的属性吗?(怎么做?),ruby-on-rails,Ruby On Rails,我为一个类生成了脚手架:ExpenseReport 其中一个属性是type——我为它创建了一个selct,其中有两个选项,可能的话:Major和Regular 提交表单以创建报告时,我遇到了以下错误: 无效的单表继承类型:Regular不是ExpenseReport的子类 我的假设是:“好吧,我们不要使用名为type的属性,它看起来可能会导致问题。”因此我创建了一个迁移,将type重命名为“report type”,并进行了rake(请参阅下面的迁移和rake证明) 迁移 class Updae
ExpenseReport
其中一个属性是type
——我为它创建了一个selct,其中有两个选项,可能的话:Major
和Regular
提交表单以创建报告时,我遇到了以下错误:
无效的单表继承类型:Regular不是ExpenseReport的子类
我的假设是:“好吧,我们不要使用名为type的属性,它看起来可能会导致问题。”因此我创建了一个迁移,将type重命名为“report type”,并进行了rake(请参阅下面的迁移和rake证明)
迁移
class UpdaeColumnName < ActiveRecord::Migration
def change
rename_column :expense_reports, :type, :report_type
end
end
但现在,它永远不会保存我的输入,每次提交时都会触发验证,告诉我“列表中不包括报告类型”,是否还有其他属性名称需要更新或其他什么
相关_form.html.erb
<div class="field">
<%= f.label :report_type %><br>
<%= f.select :report_type, ExpenseReport::TYPES,
prompt: "select one" %>
</div>
型号
class ExpenseReport < ActiveRecord::Base
validates :place_of_purchase, :items, :reason, :estimated_cost,
:requestor_name, presence: true
TYPES = [ 'Major', 'Regular' ]
validates :report_type, inclusion: TYPES
SITES = [ '001 (Lubbock)', '002 (Odessa)', '003 (Midland)', '004 (Lubbock)' ]
validates :site, inclusion: SITES
end
class ExpenseReport
属性“type”用于单表继承,更多信息可在此处找到:或在此处:
如果你坚持你的新报告类型,你应该改变你的脚手架材料。你在轨道4上吗?如果是,请在expense_reports_controller.rb中更改您的私人expense_report_params方法
应该是这样的:
def expense_report_params
params.require(:expense_report).permit(:place_of_purchase, :items, :reason, :estimated_cost, :requestor_name, :other_attributes, :type)
end
将其更改为:
def expense_report_params
params.require(:expense_report).permit(:place_of_purchase, :items, :reason, :estimated_cost, :requestor_name, :other_attributes, :report_type)
end
在rails 4中,您必须始终允许您的参数,否则它将无法工作。如果您允许一个不存在的参数“type”,您将得到一个错误。@DaveNewton刚刚做了;还是没有骰子。值得一提的是,我在
站点上做了同样的事情(在模型中也可以看到),这很好。谢谢你的回复!
def expense_report_params
params.require(:expense_report).permit(:place_of_purchase, :items, :reason, :estimated_cost, :requestor_name, :other_attributes, :report_type)
end