Ruby on Rails, Two Models in One Form

Ruby on Rails, two models in one form

Use fields_for for the associated models.

There should be no square brackets arround the parameters of fields_for

In your code example, I cannot find the relation between Patient and Diagnosis, and the plural of diagnosis is diagnoses, you can specify this in config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'diagnosis','diagnoses'
end

So your Patient model should contain

class Patient < ActiveRecord::Base
attr_accessible :age, :name, :city, :street, :number
has_many :diagnoses
end

And you can write in your form:

 <div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<%= fields_for(@patient, @patient.diagnoses.build) do |u| %>
<div class="field">
<%= u.label :content %><br />
<%= u.text_field :content %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>

How to handle multiple models in one rails form?

You can handle as many forms as you need, if you use the fields_for helper properly.

This is where you're falling short I think (your controller seems okay).

I also wrote an answer about this some time back.

#app/models/survey.rb
class Survey < ActiveRecord::Base
has_many :sections
accepts_nested_attributes_for :sections
end

#app/models/section.rb
class Section < ActiveRecord::Base
belongs_to :survey
has_many :questions
accepts_nested_attributes_for :questions
end

#app/models/question.rb
class Question < ActiveRecord::Base
belongs_to :section
has_many :answers
end

Try and keep your model names as succinct as possible.

#app/controllers/surveys_controller.rb
class SurveysController < ApplicationController
def new
@survey = Survey.new
@survey.sections.build.questions.build
end

def create
@survey = Survey.new survey_params
@survey.save
end

private

def survey_params
params.require(:survey).permit(:title, sections_attributes: [:title, questions_attributes:[:title]])
end
end

#app/views/surveys/new.html.erb
<%= form_for @survey do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :sections do |section| %>
<%= section.text_field :title %>
<%= section.fields_for :questions do |question| %>
<%= question.text_field :title %>
<% end %>
<% end %>
<%= f.submit %>
<% end %>

Rails - one form to two models

You'll be best looking into accepts_nested_attributes_for:

#app/models/store.rb
class Store < ActiveRecord::Base
has_one :address
accepts_nested_attributes_for :address
end

#app/controllers/stores_controller.rb
class StoresController < ApplicationController
def new
@store = current_user.stores.new
@store.build_address
end

def create
@store = current_user.stores.new store_params
@store.save
end

private

def store_params
params.require(:store).permit(:store_type_id, :latitude, :longitude, :name, :notes, address_attributes: [:line_1])
end
end

#app/views/stores/new.html.erb
<%= form_for @store do |f| %>
<%= f.fields_for :address do |a| %>
<%= a.text_field :line_1 %>
<% end %>
<%= f.submit %>
<% end %>

One form to update multiple models

You can use rails nested attributes http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html or create a form object http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

Ruby on Rails : Updating multiple models in a single form

I think this change in app/models/order.rb should do the trick:

class Order < ActiveRecord::Base
belongs_to :user
belongs_to :from_house, class_name: 'House'
belongs_to :to_house, class_name: 'House'

accepts_nested_attributes_for :user, :from_house, :to_house

validates :user, :from_house, :to_house, presence: true

def from_house_attributes=(attributes)
fh = build_from_house(attributes)
fh.user = self.user
end

def to_house_attributes=(attributes)
th = build_to_house(attributes)
th.user = self.user
end
end

Now, try this in your Rails console:

params = { user_attributes: { name: 'New name', email: 'name@example.com' }, from_house_attributes: { name: 'From house name' }, to_house_attributes: { name: 'to house name' } }
o = Order.new(params)
o.save
o.from_house
o.to_house

Cheers!

Ruby on Rails: create records for multiple models with one form and one submit

To submit a form and it's associated children you need to use accepts_nested_attributes_for

To do this, you need to declare it at the model for the controller you are going to use (in your case, it looks like the Quote Controller.

class Quote < ActiveRecord::Base
attr_accessible :quote_number
has_one :customer
has_one :item
accepts_nested_attributes_for :customers, :items
end

Also, you need to make sure you declare which attributes are accessible so you avoid other mass assignment errors.



Related Topics



Leave a reply



Submit