Skip to content

Instantly share code, notes, and snippets.

@Liooo
Last active August 29, 2015 14:05
Show Gist options
  • Save Liooo/2037775c47ce3e1fe6ab to your computer and use it in GitHub Desktop.
Save Liooo/2037775c47ce3e1fe6ab to your computer and use it in GitHub Desktop.
#======= controller
def new
@model_a = ModelA.new
@model_a_param = ModelAParam.new
end
def create
@model_a_param = params[:model_a_param] # strong_parameterは省略ね
if @model_a_param.invalid?
render action: new
return
end
# ...
# ... model_a_param をごにょごにょ
# ...
end
#======== _form.html.erb
<%= form_for @model_a_param do |f|
<%= f.input :attr_a %>
<%= f.input :attr_b %>
<%= f.input :attr_c %>
<!-- ... submit button etc-->
<% end %>
#======== RAILS_ROOT/app/params/model_a.rb とか
class ModelAParam << BaseParamModel
validates_presence_of :param_a, :param_b, :param_c
attr_accessor :param_a, :param_b, :param_c
end
#======== ちなみに
class BaseParamModel
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment