在rails中,您將實現所謂的「Form」模型。對於Rails新手來說,這會向您介紹許多奇怪的「半高級」主題,但以下是您想要做的事情。我建議您查找任何您沒看過的方法/模塊,因爲在這裏背景中有一些神奇的導軌使用(驗證回調等):
首先選擇一個類名...根據您提供的屬性我會打電話給班級公司。
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中編寫類的教程,並熟悉面向對象編程的基本概念。 –