レールでは、「フォーム」モデルとして知られているものを実装することになります。レールに慣れていないので、多くの奇妙な「セミアドバンス」のトピックを紹介しますが、以下はあなたがしたいことです。私はあなたが見たことのないメソッド/モジュールを探してみることをお勧めします。ここでは背景にある魔法のレールの使用方法(検証コールバックなど)があります:
最初にクラス名を選択してください...あなたの属性に基づいて私はクラス会社に電話します。あなたが通常の流れに従っている場合
class Company
include ActiveModel::Model #This gives you access to the model methods you're used to.
include ActiveModel::Validations::Callbacks #Needed for before_validation
attr_accessor :address
attr_accessor :images
attr_accessor :facilities #This accessor method basically just says you will be using "facilities" as a virtual attribute and it will allow you to define it.
#Add validations here as needed if you're taking these values from form inputs
def initialize(params={}) #This will be executed when you call Company.new in your controller
self.facilities=params[:facilities]
#etc for the other fields you want to define just make sure you added them above with attr_accessor or you wont be able to define them. Attr_accessor is a pure ruby method if it's new to you.
end
def another_method
unless self.facilities.somethingYourCheckingForLikeNil?
errors.add(:facilities, "This failed and what you checked for is not here!"
end
end
end
は、その後、あなたのコントローラであなたのようなものだろう:私はあなたに完全な例を与えることはできませんより多くの情報がなければ
def new
company = Company.new
end
def create
company = Company.new(company_params)
#whatever logic here for saving what you may want saved to a database table etc...
end
private
def company_params
params.require(:company).permit(:facilities, :address, :whatever_other_params_etc)
end
をしかし、これはあなたに軌道に乗る必要があります「フォームモデル」の詳細をご覧ください。
これはRubyの基本的なオブジェクト指向プログラミングです。 Rubyでクラスを書く際のチュートリアルを探し、オブジェクト指向プログラミングの基本概念に慣れさせることをお勧めします。 –