I have two models in my app: "WorkPost" and "Contacts".
WorkPost
class WorkPost < ActiveRecord::Base
has_one :contacts
end
Contacts
class Contacts < ActiveRecord::Base
belongs_to :work_post
end
In my controller's new method I do:
def new
#work_post = WorkPost.new
#work_post.contacts
end
And in view I create form:
<%= form_for(#work_post) do |f| %>
<div class="field">
<%= f.label 'Vacation' %><br>
<%= f.text_field :post_title, :placeholder => 'Vacation here' %>
</div>
<div class="field">
<%= f.label 'Vacation description' %><br>
<%= f.text_area :post_body, :placeholder => 'Vacation description here' %>
</div>
<% f.fields_for :contacts do |cf| %>
<div class="field">
<%= cf.label 'Email' %><br>
<%= cf.text_field :emails, :placeholder => 'Email here' %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Post vacation", :class => 'btn_act' %>
</div>
<% end %>
But it seems like line <% f.fields_for :contacts do |cf| %> doesn't work.
Everything is rendered fine but email field.What I am doing wrong?
The problem is with this line
<% f.fields_for :contacts do |cf| %>
which should be
<%= f.fields_for :contact do |cf| %>
Also, the class name for the model and the association name for has_one/belongs_to should be singular.
#work_post.rb
class WorkPost < ActiveRecord::Base
has_one :contact #should be singular
end
#contact.rb
class Contact < ActiveRecord::Base #should be singular
belongs_to :work_post
end
Also, notice the change :contacts to :contact, as it is a has_one association.
Update:
Also, try the below changes
Include accepts_nested_attributes_for :contact in work_post.rb model
#work_post.rb
class WorkPost < ActiveRecord::Base
has_one :contact
accepts_nested_attributes_for :contact
end
Change the new method to below
def new
#work_post = WorkPost.new
#work_post.build_contact
end
Related
I have three models
class Property < ActiveRecord::Base
has_many :contact
accepts_nested_attributes_for :contact
has_many :business
accepts_nested_attributes_for :business
end
class Business < ActiveRecord::Base
belongs_to :property
has_many :contact
end
class Contact < ActiveRecord::Base
belongs_to :property
belongs_to :business
end
I created a form that creates the Property with a nested contact and a nested business, how can I get that business to have a nested contact?
Here is my form
<%= form_for(#property) do |f| %>
<div class="field">
<%= f.label :address %><br>
<%= f.text_field :address %>
</div>
<% end %>
<%= f.fields_for :contact do |contact_form| %>
<div class="field">
<%= f.label :contact_title, "Title" %><br>
<%= contact_form.text_field :title %><br>
<%= f.label :contact_name, "Name" %><br>
<%= contact_form.text_field :name %><br>
</div>
<% end %>
<%= f.fields_for :business do | business_form| %>
<div class="indv-biz field">
<%= f.label :business_name, "Name" %><br>
<%= business_form.text_field :name %><br>
</div>
<div class="business-contact">
<p>Business Contact</p>
<%= f.fields_for :business_contact do | business_contact | %>
<div class="field">
<%= business_contact.label :contact_title, "Title" %><br>
<%= business_contact.text_field :title %><br>
<% end %>
<% end %>
I can get it to save so the business is connected to the property and the contact is connected to the property but I can't figure out how to get a contact connected to the business
Thanks
You should try deep nesting like this. your requirement is a property have many business which in-turn have many contacts. In this case, you actually should do is setting nested form for property with business and that business should have nested form of contacts. The below one will work for you.
Form
nested_form_for #property do |f|
...
f.fields_for :bussiness do |bussiness_form|
...
bussiness_form.fields_for :contact_form do |contact_form|
....
end
end
end
end
Models
class Property < ActiveRecord::Base
has_many :contact
has_many :business
accepts_nested_attributes_for :business
end
class Business < ActiveRecord::Base
belongs_to :property
has_many :contacts
accepts_nested_attributes_for :contacts
end
class Contact < ActiveRecord::Base
belongs_to :property
belongs_to :business
end
controller
def property_params
params.require(:property).permit(:id,.., :bussiness_attributes => [:id,.., , :contacts_attributes => [:id, ..]])
end
I have a User model as below,
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable
has_many :company_users
accepts_nested_attributes_for :company_users, :allow_destroy => true
has_many :companies, :through => :company_users
has_many :roles, :through => :company_users
end
and its associated model CompanyUser as below,
class CompanyUser < ActiveRecord::Base
belongs_to :user
belongs_to :company
belongs_to :role
end
I am trying to build the associations as below but it seems like not working
# GET /users/new
def new
#user = User.new
#user.company_users.build
end
View file is as follows,
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<h3>Companies and Roles</h3>
<div class="field">
<% f.fields_for :company_users do |cu| %>
<p>
<%= cu.label :company_id %>
<%= cu.text_field :company_id%>
<%= cu.label :role_id %>
<%= cu.text_field :role_id %>
<%= cu.check_box :_destroy %>
<%= cu.label :_destroy, 'delete' %>
</p>
<% end %>
<p>
<%= f.submit 'Add to user', :name => "add_company_user" %>
<%= f.submit 'Delete from user', :name => "remove_company_user" %>
</p>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I am trying to figure out where it is going wrong.
You are missing a =, therefore the fields_for block isn't rendered to the page. Change this line
<% f.fields_for :company_users do |cu| %>
to
<%= f.fields_for :company_users do |cu| %>
I have two models that I would like to create with one form. I tried following this railscasts tutorial, but I just can't get the nested fields to display on the form. How can I make these nested fields appear?
Models
class Poll < ActiveRecord::Base
has_many :poll_answers, :dependent => :destroy
accepts_nested_attributes_for :poll_answers, allow_destroy: true
end
class PollAnswer < ActiveRecord::Base
belongs_to :poll
end
Controller
class PollsController < ApplicationController
def new
#poll = Poll.new
2.times { #poll.poll_answers.build }
end
private
def poll_params
params.require(:poll).permit([
:question,
poll_answers_attributes: [:answer]
])
end
end
View
<%= form_for(#poll) do |f| %>
<div class="field">
<%= f.label :question %><br>
<%= f.text_field :question %>
</div>
<% f.fields_for :poll_answers do |pa| %>
<p>Hello
<%= pa.text_field :answer %>
</p>
<% end %>
<%= debug #poll.poll_answers %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You missed an =
<%= f.fields_for :poll_answers do |pa| %>
<p>Hello
<%= pa.text_field :answer %>
</p>
<% end %>
HI I am trying to implement the MTI in my application. I have a Person Model and 2 models inheriting from it: Client and TeamMember. When creating a Team Member I want to save to to database vallues for both person (first and last name, email etc) and team member(experience level, type of team, if lead or not). I am using the nested attributes form so in my team member form I am nesting the person fields. Unfortunatellly I am getting "Can't mass-assign protected attributes: person" error when trying to save. Can anyone tell me how this can be solved? Thanks!
Models:
UPDATED TeamMember class but still the same error
also tried people_attributes and persons_attributes and none of these worked
class TeamMember < ActiveRecord::Base
has_many :project_team_members
has_many :projects, through: :project_team_members
has_one :person, as: :profile, dependent: :destroy
accepts_nested_attributes_for :person
attr_accessible :person_attributes, :experience_level, :lead, :qualification, :team
end
class Person < ActiveRecord::Base
belongs_to :company
belongs_to :profile, polymorphic: true
attr_accessible :email, :first_name, :last_name, :phone_number, :profile_id, :profile_type
end
Controller as follows:
class TeamMembersController < ApplicationController
def create
person = Person.create! { |p| p.profile = TeamMember.create!(params[:team_member]) }
redirect_to root_url
end
and the view:
<%= form_for(#team_member) do |f| %>
<%= f.fields_for :person do |ff| %>
<div>
<%= ff.label :first_name %>
<%= ff.text_field :first_name %>
</div>
<div>
<%= ff.label :last_name %>
<%= ff.text_field :last_name %>
</div>
<div>
<%= ff.label :phone_number %>
<%= ff.text_field :phone_number %>
</div>
<div>
<%= ff.label :email %>
<%= ff.text_field :email %>
</div>
<div>
<%= ff.label :company_id %>
<%= ff.text_field :company_id %>
</div>
<% end %>
<div class="field">
<%= f.label :team %><br />
<%= f.text_field :team %>
</div>
<div class="field">
<%= f.label :experience_level %><br />
<%= f.text_field :experience_level %>
</div>
<div class="field">
<%= f.label :qualification %><br />
<%= f.text_field :qualification %>
</div>
<div class="field">
<%= f.label :lead %><br />
<%= f.check_box :lead %>
</div>
<div class="actions">
<%= f.submit %>
</div>
UPDATED TeamMembersController (Solution thanks to the courtesy of Tiago)
def new
#team_member = TeamMember.new
#team_member.build_person
respond_to do |format|
format.html # new.html.erb
format.json { render json: #team_member }
end
end
def create
#team_member = TeamMember.create!(params[:team_member])
redirect_to root_url
end
To mass assign attributes in a nested form, you'll need to specify:
class TeamMember < ActiveRecord::Base
has_many :project_team_members
has_many :projects, through: :project_team_members
has_one :person, as: :profile, dependent: :destroy
:experience_level, :lead, :qualification, :team #what is this line doing??
accepts_nested_attributes_for :person
attr_accessible :person_attributes
end
EDIT:
In the action called before the form you need to build person. Like:
#team_member = TeamMember.new
#team_member.build_person
Then you'll have one person (non-persisted) associated with #team_member.
I have 2 models: News and Uploadedfile
class News < ActiveRecord::Base
has_many :uploadedfiles, as: :parent
attr_accessible :title, :content, :author
end
class Uploadedfile < ActiveRecord::Base
belongs_to :parent, polymorphic: true
has_attached_file :url
attr_accessible :url_file_name, :url_content_type, :url_file_size, :url_updated_at
end
And form:
<%= form_for(#news) do |f| %>
<div class="field">
<%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>
<%= uf.label :url %><br>
<%= uf.file_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When i'm submitting form, my table uploadedfile is not changed
where is the problem? thank you!
I think you have nested arribute :uploadedfiles
class News < ActiveRecord::Base
has_many :uploadedfiles, as: :parent
attr_accessible :title, :content, :author, :uploadedfiles_attributes
accept_nested_attributes_for :uploadedfiles
end
And in form :
change:
<%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>
to:
<%= f.fields_for :uploadedfiles, Uploadedfile.new do |uf| %>
I don't think you need polymorphic association here. Here is a more readable way of doing this:
class News < ActiveRecord::Base
has_many :uploadedfiles
attr_accessible :title, :content, :author
accept_nested_attributes_for :uploadedfiles
end
class Uploadedfile < ActiveRecord::Base
belongs_to :news
has_attached_file :url
attr_accessible :url_file_name, :url_content_type, :url_file_size, :url_updated_at
end
*note that I've added accept_nested_attributes_for
And the form:
<%= form_for(#news) do |f| %>
<div class="field">
<%= f.fields_for :uploadedfiles do |uf| %>
<%= uf.label :url %><br>
<%= uf.file_field :url %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>