あなたはレールで何でもできます!私の意見では、このフォームには多くのことが起こり、アプリケーションの1つのビューに対してバリデーションなどでいくつかのモデルを掘り下げたくないので、フォームモデルとして知られているものを作成するのが最良のアプローチです。これを行うには、基本的にこの情報をすべて取り込み、必要な検証を実行し、必要なレコードを作成するクラスを作成します。これを行うには、モデルフォルダにso_much.rb
という名前の新しいファイルを作成してください(ファイル名と同じ名前を付けて、Railsが自動的に見つけられるようにすることができます)。
あなたのそばで。 RBファイルを実行します。
class SoMuch
include ActiveModel::Model #This gives us rails validations & model helpers
attr_accessor :visual_title
attr_accessor :visual_cover #These are virtual attributes so you can make as many as needed to handle all of your form fields. Obviously these aren't tied to a database table so we'll run our validations and then save them to their proper models as needed below!
#Add whatever other form fields youll have
validate :some_validator_i_made
def initialize(params={})
self.visual_title = params[:visual_title]
self.visual_cover = params[:visual_cover]
#Assign whatever fields you added here
end
def some_validator_i_made
if self.visual_title.blank?
errors.add(:visual_title, "This can't be blank!")
end
end
end
今、あなたは、このフォームを処理しているあなたのコントローラに行くことができると同じような何かを::次に、あなたのビューで、あなたは次のよう@so_much
であなたのform_forを設定します
def new
@so_much = SoMuch.new
end
def create
user_input = SoMuch.new(form_params)
if user_input.valid? #This runs our validations before we try to save
#Save the params to their appropriate models
else
@errors = user_input.errors
end
end
private
def form_params
params.require(@so_much).permit(all your virtual attributes we just made here)
end
の
<%= form_for @so_much do %>
whatever virtual attributes etc
<% end %>
フォームモデルは、Railsの中に少し進んでいるが、それはあなたが一つのモデルのフォームの多くの異なる種類があり、混乱のすべてを望んでいない大きなアプリケーションになると命の恩人です。
はい、レールはこれで素晴らしいです。 'accepts_nested_attributes_for'はあなたが読み上げる必要があるものです。 – Swards
複雑なフォームを作成するには、rails guideを参照してください。[http://guides.rubyonrails.org/form_helpers.html#building-complex-forms] – johnnynemonic