我想同时更新两个不同模型中单个表单的值,我编写了以下代码: -
if @mess.update_attributes!(:mess_name => params[:mess_name_update], :mess_capacity => params[:mess_capacity_update]) && @mess_price.update_attributes!(:breakfast_charge => params[:mess_breakfast_charge_update], :lunch_charge => params[:mess_lunch_charge_update], :dinner_charge => params[:mess_dinner_charge_update], :monthly_charge => params[:mess_monthly_charge_update], :semesterly_charge => params[:mess_semesterly_charge_update], :start_date => params[:start_date_update], :end_date => params[:end_date_update], :rebate => params[:rebate_update])
flash[:success] = "Mess Details Updated Successfully!!"
else
flash[:error] = "Some Error! Please Try Again!"
end
但上面的代码给出了以下错误
的ActiveRecord :: RecordInvalid
验证失败:开始日期不能为空
以下是我正在使用的两个架构, @mess 其 MessType 模型以及 @mess_price 其 MessPrice 型号:
MessType
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| mess_id | int(11) | NO | PRI | NULL | auto_increment |
| mess_name | varchar(255) | NO | | NULL | |
| mess_capacity | int(11) | NO | | NULL | |
| start_date | date | No | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
MessPrice
+-------------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| breakfast_charge | float | YES | | NULL | |
| lunch_charge | float | YES | | NULL | |
| dinner_charge | float | YES | | NULL | |
| monthly_charge | float | YES | | NULL | |
| semesterly_charge | float | YES | | NULL | |
| rebate | float | YES | | NULL | |
| start_date | date | YES | | NULL | |
| end_date | date | YES | | NULL | |
| mess_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+-------------------+----------+------+-----+---------+----------------+
答案 0 :(得分:1)
我建议安装awesome_print
gem - https://github.com/michaeldv/awesome_print
# add to Gemfile
gem 'awesome_print'
# install
bundle
然后控制器操作中的第一件事就是
logger.debug " -----------"
logger.ap params
logger.debug " -----------"
检查您的日志文件log/development.log
是否输出,可能是params正确遇到但不是您所期望的?
某些属性可能嵌套在另一个哈希键中,需要通过params[:something][:xyz]
我还建议让代码更具可读性并在事务中运行
@mess.mess_name = params[:mess_name_update]
@mess.mess_capacity = params[:mess_capacity_update]
@mess_price.breakfast_charge = params[:mess_breakfast_charge_update]
@mess_price.lunch_charge = params[:mess_lunch_charge_update]
@mess_price.dinner_charge = params[:mess_dinner_charge_update]
@mess_price.monthly_charge = params[:mess_monthly_charge_update]
@mess_price.semesterly_charge = params[:mess_semesterly_charge_update]
@mess_price.start_date = params[:start_date_update]
@mess_price.end_date = params[:end_date_update]
@mess_price.rebate = params[:rebate_update]
# NOTE: an alternative to above is to name your html input fields the rails way
# so that params are sent in a nested hash, i.e. "mess_price[start_date]" -> params[:mess_price][:start_date]
# then you can do @mess_price.update_attributes(params[:mess_price])
# using form_for html helper will automatically apply this style of naming to html input fields
Mess.transaction do
# you might want save! vs save - depends if you show validation error in the UI or not
if @mess.save && @mess_price.save
flash[:success] = "Mess Details Updated Successfully!!"
else
# don't save changes, show validation errors
raise ActiveRecord::Rollback
flash[:error] = "Some Error! Please Try Again!"
end
end