Ruby on rails:在保存记录更改时出现问题

时间:2015-08-04 06:15:29

标签: ruby-on-rails ruby

我有一个脚手架控制器,我在更新操作中有:

def update
    if @offer.update(offer_params)
        redirect_to @offer
    else
        render "edit"
    end 
end
def edit

end

这段代码给了我一个错误:

undefined method `main_id_changed?' for #<Offer:0x00000004206c10>

Destroy,Create,Show等其他动作效果很好。

有人遇到过这样的问题吗?

提前谢谢!

编辑:

在控制器中:

before_action :find_offer, only: [:show, :edit, :update, :destroy]

private

def offer_params
    params.require(:offer).permit(:title, :company, :description, :telephone, :adress, :economy, :url, :category_id, :image, :lat, :lng, :image_file_name, :city_id)
end

def find_offer
    @offer = Offer.find(params[:id])
end

模特:

class Offer < ActiveRecord::Base
belongs_to :category
belongs_to :main, counter_cache: true
belongs_to :city

has_attached_file :image, 
    styles: { thumb: "64x64", small: "100x100>", medium: "300x210#", large: "600x600" },
    url: '/img/:class/:id/:style.:extension',
    path: ':rails_root/public:url'

validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

1 个答案:

答案 0 :(得分:0)

id方法

中允许offer_params字段
params.require(:offer).permit(:id, :title, :company, :description, :telephone, :adress, :economy, :url, :category_id, :image, :lat, :lng, :image_file_name, :city_id)

并删除counter_cache: true

希望有所帮助!