0

我想同时更新从两个不同模型中的单个表单中获取的值,为此我编写了以下代码:-

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

验证失败:开始日期不能为空

以下是我使用的两个模式,@messMessType模型和@mess_priceMessPrice模型:

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    |                |
+-------------------+----------+------+-----+---------+----------------+
4

1 回答 1

1

我建议安装awesome_printgem - 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[: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
于 2013-06-29T14:27:41.283 回答