将attr_accessible从Rails 3升级到Rails 4

时间:2014-08-08 07:49:44

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 upgrade

我试图系统地从rails 3升级到rails 4,我的所有25个型号都基于attr_accessor!所以在进入之前,任何人都可以为我提供一个如何做到这一点的简单示例。我已经阅读了文档和其他主题,但由于这是我的第一次升级Rodeo,因此不清楚如何操作。

class Settings < ActiveRecord::Base
  image_accessor :favicon
  attr_accessible :company_name, :show_hot_jobs, :show_students, :subheading, :show_testimonials, :show_on_boarding, :max_concurrent_applications
  attr_accessible :image_uid, :max_concurrent_application_groups
  attr_accessible :primary_color, :white_color, :gray_color, :opacity, :locale, :lang_nl, :lang_fr, :lang_de, :lang_en, :privacy_page
  attr_accessible :show_evp, :show_contact_person, :show_jobs_for_you
  attr_accessible :favicon, :favicon_uid, :remove_favicon, :retained_favicon
  attr_accessible :home_url, :show_correspondence, :show_appointment
  attr_accessible :sliderone_uid, :slidertwo_uid, :sliderthree_uid, :sliderfour_uid, :sliderfive_uid
  attr_accessible :sliderone_link, :slidertwo_link, :sliderthree_link, :sliderfour_link, :sliderfive_link
  attr_accessible :sliderone_testoverview,  :slidertwo_testoverview, :sliderthree_testoverview, :sliderfour_testoverview, :sliderfive_testoverview
  attr_accessible :sliderone_page,  :slidertwo_page, :sliderthree_page, :sliderfour_page, :sliderfive_page

  validate :any_lang_present?
  validates :max_concurrent_applications, :numericality => { :greater_than_equal_to => 1 }
  validates :max_concurrent_application_groups, :numericality => { :greater_than_equal_to => 1 }

  # Fav Icon Validation
  validates_property :ext, of: :favicon, :in => ['ico', 'png', 'gif']

  has_paper_trail

  has_many :setting_translations, :foreign_key => :setting_id
  accepts_nested_attributes_for :setting_translations, :allow_destroy => true, :reject_if => :all_blank
  attr_accessible :setting_translations_attributes, :allow_destroy => true

  translates :subheading, :company_name, :image_uid, :home_url, :sliderone_uid, :slidertwo_uid, :sliderthree_uid, :sliderfour_uid, :sliderfive_uid
  translates :sliderone_link, :slidertwo_link, :sliderthree_link, :sliderfour_link, :sliderfive_link
  translates :sliderone_testoverview,  :slidertwo_testoverview, :sliderthree_testoverview, :sliderfour_testoverview, :sliderfive_testoverview
  translates :sliderone_page,  :slidertwo_page, :sliderthree_page, :sliderfour_page, :sliderfive_page

2 个答案:

答案 0 :(得分:1)

attr_accessible可以这样转换:

class Settings
  attr_accessible :home_url
  accepts_nested_attributes_for :setting_translations
end

class SettingTranslation
  attr_accessible :etc
end

class SettingsController

  def create
    @settings = Settings.new(settings_params)
    # ...
  end

  private

  def settings_params
    params.require(:settings).permit(
      :home_url,
      :setting_translations_attributes => [:id, :_destroy, :etc]
    )
  end
end

注意,如果要在该模型上允许销毁:_destroy),则必须包含:allow_destroy => true,并且必须包含应该可以从任何嵌套属性访问的所有属性。虽然您在被许可时删除了attr_accessible,但您删除accepts_nested_attributes_for

答案 1 :(得分:0)

只需从模型中删除attr_accessible即可。并根据需要在控制器中添加许可证参数。 如下所示:

class SupportTicketsController < ApplicationController

  def create
    @support_ticket = SupportTicket.create(house_params)
    ......
  end

  private

  def house_params
    params.require(:support_ticket).permit(:subject, :message, ....)
  end

end

如果您不想进行这么多更改,请添加&#34; protected_attributes&#34;你的gemfile中的gem https://github.com/rails/protected_attributes并且一切都会像以前一样工作。