I'm following the docs and toying with simple_form, formtastic, and nested_form gems with no success. This very simple example yields empty for for me:
resort.rb
class Resort < ActiveRecord::Base
attr_accessible :address, :description, :latitude, :longitude, :name, :phone, :second_name,
:resort_type_id, :infrastructure_attributes
validates_presence_of :name, :address, :phone, :description
has_many :infrastructures
belongs_to :resort_type
accepts_nested_attributes_for :infrastructures
end
infrastructure.rb
class Infrastructure < ActiveRecord::Base
attr_accessible :name, :description, :infrastructure_type_id, :resort_id
belongs_to :resort
belongs_to :infrastructure_type
end
form view
= form_for #resort do |f|
= f.fields_for :infrastructures do |i|
= i.text_field :name
Seems I missed something obvious but can't figure out what exactly is wrong with the code.
If I may ask, in your controller action did you build the appropriate code for your infrastructure. Something like
3.times { #resort = #resort.infrastrucures.build }
To my knowledge you need something like this for your form before it can build the proper nested form content
Related
Newbie Rails user here so sorry if I'm totally overlooking something; I'm just looking for direction on what resources I should use to solve this problem.
I have a Rails app running that uses virtual attributes to define a complicated has_many :through relationship. What I'm looking to do is to use the id of an existing entry rather than creating a new one when a user submits a form. I have attempted to use find_or_initializes_by and find_or_create to no avail. Depending on how I set it up, it either changes one of the diagnosis columns on the join table successfully (but leaves the other diagnosis column blank, causing a rollback), or it doesn't work at all.
Background for my use case: a Case can have many diagnoses (Diagnosis), some of which are primary, and others are "differential" diagnoses. Cases and multiple diagnoses are related in a join table.
My current setup:
case.rb
def new
#case = Case.new
#case_and_diagnoses = #case.case_and_diagnoses.build
#primary_diagnoses = #case_and_diagnoses.build_primary_diagnosis
#differential_diagnoses = #case_and_diagnoses.build_differential_diagnosis
end
The relevant section of the case form:
<%= f.fields_for :case_and_diagnoses do |x| %>
<%= x.fields_for :primary_diagnosis do |diagnosis| %>
<%= diagnosis.label :name, "Diagnosis" %>
<%= diagnosis.text_field :name %>
<% end %>
<%= x.fields_for :differential_diagnosis do |differential| %>
<%= differential.label :name, 'Differential diagnosis' %>
<%= differential.text_field :name %>
<% end %>
<% end %>
What's the best way to tackle this problem? I don't need an explicit code block to use, but I'm looking for a strategy so I can maybe find my own way. Thank you very much!
Update:
The models:
class Case < ApplicationRecord
before_save :get_diagnoses
belongs_to :submitter, class_name: 'User', foreign_key: 'submitter_id'
belongs_to :resident, class_name: 'User', foreign_key: 'resident_id'
belongs_to :faculty, class_name: 'User', foreign_key: 'faculty_id'
has_one :service
has_many :case_and_diagnoses
has_many :primary_diagnoses, through: :case_and_diagnoses
has_many :differential_diagnoses, through: :case_and_diagnoses
accepts_nested_attributes_for :case_and_diagnoses
end
class Diagnosis < ApplicationRecord
has_many :case_and_diagnoses
has_many :cases, through: :case_and_diagnoses
end
class CaseAndDiagnosis < ApplicationRecord
belongs_to :case
belongs_to :primary_diagnosis, class_name: 'Diagnosis', foreign_key: :primary_diagnosis_id, optional: true
belongs_to :differential_diagnosis, class_name: 'Diagnosis', foreign_key: :differential_diagnosis_id, optional: true
accepts_nested_attributes_for :primary_diagnosis, :differential_diagnosis
end
Hello i have problem with nested form.. im looking an hour to that and dont know what i forget ..
models/trainer.rb
class Trainer < ActiveRecord::Base
attr_accessible :telephone, :user_attributes
has_one :user
accepts_nested_attributes_for :user
end
models/user.rb
class User < ActiveRecord::Base
belongs_to :trainer
attr_accessible :email, :image_url, :name, :password_hash, :password_salt, ...
attr_accessible :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
<+ validations ...>
controllers/trainers_controller.rb
def new
#trainer = Trainer.new
#trainer.build_user
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trainer }
end
end
and i can display new trainer form view (i addedd all user columns as nested)
but when i hit CREATE i get
Can't mass-assign protected attributes: user
whats wrong ? thank you
edit: my db schema looks like
[users]
id
trainer_id
name
surname
[trainers]
telephone
Here i uploaded my simplify simple app if anyone was interested :)
https://github.com/ScottHiscock/NestedForm
Ref accepts_nested_attributes_for
Defines an attributes writer for the specified association(s).
If you are using attr_protected or attr_accessible, then you will need to add
the attribute writer to the allowed list.
So I think you have to do the following:
class Trainer < ActiveRecord::Base
attr_accessible :telephone, :user_attributes
Also add user to attr_accessible list of trainer.rb model as follows
attr_accessible :telephone, :user_attributes
or try this
attr_accessible :telephone, :user
the mistake was in the view, i had
<%= f.fields_for :users do |u| %>
but correct is
<%= f.fields_for :user do |u| %>
:-)
i have the following nested form :
<%= form_for #contrat,:url => backend_orders_update_report_url(#contrat) do |f| %>
<%= f.fields_for :contrat_lines do |fcl| %>
<%= fcl.object.inspect %>
<% end %>
<% end %>
the output is the following :
nil
In the nested forms i want to display a few elements not as form but as raw text and a few ones as form field. Usually in a form by doing f.object.name, i would access the name and be able to display it as I want. However, here if i do fcl.object, there is only nil. It should display the inspection of a contrat_line object.
Is it possible to access the data in a nested form?
EDIT :
the controller action :
def show_report
#contrat = Contrat.find(params[:id])
end
Here is what models look like with the relation at the beggining :
ContratLine :
class ContratLine < ActiveRecord::Base
include Priceable
belongs_to :contrat
belongs_to :good
#a enlever ici et dans la base
attr_accessible :active_start,:active,:good_id,:pricing,:contrat
validates :active_start, :presence=> true,:if => "active"
validate :active_start_smaller_than_active_stop
validate :active_start_day_cannot_be_greater_than_28
has_one :pricing, :as => :priceable, :dependent => :delete
before_validation :convert_month_year_to_date
after_save :set_user_subscription_date
Contrat :
class Contrat < ActiveRecord::Base
has_many :contrat_lines, :dependent => :delete_all
belongs_to :user
attr_accessible :user_id,:pricing_id,:state,:adresse_id,:adresse,:payment,:adresse_attributes,:automatic,:start_date,:end_date
enum_attr :state, %w(basket waiting_data to_confirm paid) do
labels :basket=>'Panier', :to_confirm=>'Non payƩ',:paid=>'PayƩ'
end
enum_attr :payment, %w(debit_card wire_transfer cheque direct_debit)
belongs_to :adresse
accepts_nested_attributes_for :adresse, :allow_destroy => true
scope :by_state, lambda { |state| where("state = ?",state) }
scope :last_automatic, where("automatic = true").order("invoice_date DESC")
scope :last_with_adresse, where("state != 'basket'").order("invoice_date DESC")
before_validation :set_numbers
You're missing an accepts_nested_attributes_for :contrat_lines as well as :contrat_lines_attributes in attr_accessible
Im building a report system which uses a sort of meta question model. Questions are previusly saved in the database, and then depending of the type of report some questions are taken from the database.
Wanting to keep things DRY, i'm trying to figure out a way to pass the information of the Variable model to my report_header with no avail.
In the new action i have:
reportBody = #report_head.report_bodies.build(:variable_id => a.id)
#report_head.variables #modified, thx.
all i need is to pass the attributes from the Variable to report_head in a DRY way.
If you need to know my models:
class Variable < ActiveRecord::Base
attr_accessible :id,:break_point, :description, :name, :time_frequency, :v_type
has_many :report_bodies
has_many :report_heads, :through => :report_bodies
end
class ReportHead < ActiveRecord::Base
attr_accessible :email, :name , :report_bodies_attributes, :report_bodies, :variables_attributes
has_many :report_bodies
has_many :variables, :through => :report_bodies
accepts_nested_attributes_for :report_bodies
end
class ReportBody < ActiveRecord::Base
attr_accessible :report_head_id, :variable_value, :variable_id, :variables_attributes, :report_heads
belongs_to :report_head
belongs_to :variable
end
Update
I updated the model as suggested, and modified the way to call the variables. However im still confused about how to use it in the view, if i do something like:
<%= f.fields_for :variables do |variable| %>
<%= variable.text_field :name, :value => :name, :class => 'text_field' %>
<% end %>
it prints a string instead of the actual name.
You have define wrong name association, your association of ReportBody should be:
belongs_to :report_head
belongs_to :variable
This is not correct:
#report_head.report_bodies.build(:variable_id => a.id,:report_head_id =>#report_head.id)
chang it to:
#report_head.variables.build(:variable_id => a.id)
it's better, you don't have to set report_head_id. And this is wrong:
#report_head.report_bodies.variables
If you want to get all variables belong to #report_head, you just need using:
#report_head.variables
I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
In a view:
= form_for #user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.
The doc epochwolf cited states in the first line "Nested attributes allow you to save attributes on associated records through the parent." (my emphasis).
You might be interested in this other SO question which is along the same lines as this one. It describes two possible solutions: 1) moving the accepts_nested_attributes to the other side of the relationship (in this case, Organization), or 2) using the build method to build the Organization in the User before rendering the form.
I also found a gist that describes a potential solution for using accepts_nested_attributes with a belongs_to relationship if you're willing to deal with a little extra code. This uses the build method as well.
For belongs_to association in Rails 3.2, nested model needs the following two steps:
(1) Add new attr_accessible to your child-model (User model).
accepts_nested_attributes_for :organization
attr_accessible :organization_attributes
(2) Add #user.build_organization to your child-controller (User controller) in order to create column organization.
def new
#user = User.new
#user.build_organization
end
For Ruby on Rails 5.2.1
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
Just got to your controller, suppose to be "users_controller.rb":
Class UsersController < ApplicationController
def new
#user = User.new
#user.build_organization
end
end
And the view just as Nick did:
= form_for #user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
At end we see that #user3551164 have already solved, but now (Ruby on Rails 5.2.1) we don't need the attr_accessible :organization_attributes