Nested Form Rails - ruby-on-rails

I followed Episode 196 from Railcasts but seem that if i follow his it doesn't work quite yet, maybe cause the code is old or i just don't get rails.
Here I have 3 models
Customer Book Book_Manager
id id id
first description customer_id
last book_manager_id visible
email
password
Here are my relationship
Book
belongs_to :book_manager
def customer
book_manager.customer
end
Customer
has_many :book_managers, :dependent => :destroy
accepts_nested_attributes_for :book_managers
BookManager
belongs_to :customer
has_many :books, :dependent => :destroy
accepts_nested_attributes_for :books
The form is has follow
<%= form_for #bookmanager do |f| %>
<%= f.fields_for :books do |builder| %>
<div>
<%= builder.label :description %><br />
<%= builder.text_area :description, :rows => 3 %>
</div>
<% end %>
<div class="field">
<%= f.label :visible %><br />
<%= f.text_field :visible %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If i comment out the line
accepts_nested_attributes_for :books
It show the description box, if however i place it there the box does disapear. Did i miss something??

I think you have to pass a book object here. Try
<%= f.fields_for :books, Book.new do |builder| %>

Related

Rails has_many :through create association and associated model on parent "show" view

I have three models in my rails 5 application... What I want to do is to be able to create a note on the person's "show" view, then the note should automatically link to that person and any other person I choose to attach the note to.
Here's where I got up to
Note
class Note < ApplicationRecord
has_many :person_notes
has_many :people, through: :person_notes
accepts_nested_attributes_for :person_notes
end
Person Note (Join table)
class PersonNote < ApplicationRecord
belongs_to :person
belongs_to :note
end
Person
class Person < ApplicationRecord
has_many :person_notes
has_many :notes, through: :person_notes
accepts_nested_attributes_for :person_notes
accepts_nested_attributes_for :notes
end
In my "show" section of my "people" controller I have the following:
def show
#notep = Note.new()
#person.notes << #notep
end
This is creating a join of a new note to my person record every time I open the page (obviously i only want the join to happen on the note I have created.)
I have rendered this in a modal as a partial in my "show" view (there is an "end" and a submit button but it's in between 3 divs and I know they weren't the cause of the issue so i didn't include them.:
<%= simple_form_for(#notep, remote: true) do |f| %>
<%= f.error_notification %>
<%= f.input :subject %>
<%= f.input :note %>
<%= f.input :date, html5: true %>
<div class="input-field">
<%= f.check_box :isalert, :id => "alert" %>
<%= f.label :isalert, :for => "alert" %>
</div>
<div class="input-field">
<%= f.check_box :archived, :id => "archived" %>
<%= f.label :archived, :for => "archived" %>
</div>
<div class="input-field">
<%= f.check_box :deleted, :id => "deleted" %>
<%= f.label :deleted, :for => "deleted" %>
</div>
<%= f.input :datecreated, html5: true %>
<%= f.input :user_id %>
<%= f.simple_fields_for :person_notes do |builder| %>
<%= builder.association :person, label_method: :lstfstfullname %>
<%= builder.input :deleted %>
<%= builder.input :datecreated, html5: true %>
<%= builder.input :user_id %>
<% end %>
You can probably tell I'm a total newb, but I'd appreciate any help I can get.
Thanks,
Leah
If I'm understanding this correctly, you'll want a separate Notes controller and create action to handle note creation. Then in your People show view, you can add a form and input field that submits to NotesController#create.

Rails .build assosciation not working rails

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| %>

Rails Polymorphic Nested Attribute

I have a polymorphic model and want to have a nested form use this model. I am getting no errors but the form is not displaying the nested field. Here are my models and stripped down form:
Polymorphic Model
class SeoMapping < ActiveRecord::Base
belongs_to :mappingtable, :polymorphic => true
attr_accessible :seo_url
validates :seo_url, :presence => true, :uniqueness => true
end
Page model using the Polymorphic Model
class Page < ActiveRecord::Base
has_one :seo_mappings, :as => :mappingtable, :dependent => :destroy
accepts_nested_attributes_for :seo_mappings
attr_accessible :content, :h1, :meta_description, :title, :seo_mappings_attributes
.........
end
Now stripped down form
<%= form_for(#page) do |f| %>
<% if #page.errors.any? %>
.......
<% end %>
<div class="field">
<%= f.fields_for :seo_mappings do |builder| %>
<%= builder.label :seo_url %><br />
<%= builder.text_field :seo_url %>
<% end %>
</div>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
.........
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I cant see why it does not display the fields_for elements. If I comment out accepts_nested_attributes_for the field then displays. Can you see where I am going wrong ?
TY
Possibly a silly question but is this the create or edit action you're talking about? And if it's create, did you call #page.build_seo_mapping or something in the controller?
Also (may be unrelated) if you use has_one, usually you want to use a singular noun, so has_one :seo_mapping instead of mappings.

Rails 3.1 - Editing Attributes in join models?

I'm having real trouble getting my head around editing attributes in has_many through join models. I've set up a very simple app to experiment with; Recipes, Ingredients and Recipe_Ingredients (the join).
Can anyone help with making this work as it should? As it is, it'll pulling through 'qty' from the join model, but not the actual ingredient.
I've put a public repo up that anyone can download to play with: https://github.com/EssentialMusic/Recipes
The models:
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_ingredients, :dependent => :destroy
has_many :recipes, :through => :recipe_ingredients
end
class Recipe < ActiveRecord::Base
attr_accessible :author, :description, :name, :recipe_ingredients_attributes, :ingredients_attributes
has_many :recipe_ingredients, :dependent => :destroy
has_many :ingredients, :through => :recipe_ingredients
accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
accepts_nested_attributes_for :recipe_ingredients
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
attr_accessible :measure, :qty, :special_instructions
end
The form
<%= form_for(#recipe) do |f| %>
<% if #recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% #recipe.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :author %><br />
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div>
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= ri.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Cheers!!
Substitute the ri with f in the second nested fields_for like this:
<%= f.fields_for :recipe_ingredients do |ri| %>
<%= ri.text_field :qty %> -
<%= **f**.fields_for :ingredients do |i| %>
<%= i.text_field :name %><br>
<% end %>
<% end %>

Deeply Nested Form Data not going into database, no errors

I'm building a multi-nested form in rails 3. I'm using the formtastic_cocoon gem, but I don't think that has much bearing on this issue.
I've got users, users have tasks, tasks have steps.
The nesting is users>tasks>steps.
I can dynamically add and remove the task fields to the user, and the step fields from the tasks.
However, when I submit the form, the user gets tasks, but the task>steps don't get saved to the database.
Rails isn't returning any errors, just nothing happens.
My models are
Class User < ActiveRecord::Base
acts_as_authentic
has_many :tasks
accepts_nested_attributes_for :tasks, :reject_if=> proc {|attributes| attributes[:entry].blank?}, :allow_destroy => true
end
Class Task < ActiveRecord::Base
attr_accessible :entry
belongs_to :user
has_many :steps
accepts_nested_attributes_for :steps, :reject_if=> proc {|attributes| attributes[:title].blank?}, :allow_destroy => true
end
Class Step < ActiveRecord::Base
attr_accesible :title
belongs_to :task
end
In my form.html.erb I have
<%= semantic_form_for #user %>
<%= form.inputs :username, :password %>
<div>
<% form.semantic_form_fields_for :tasks do |builder| %>
<%= render 'task_fields', :f=>builder %>
<% end %>
<%= link_to_add_association 'add task', form, :tasks %>
</div>
The _task_fields.html.erb looks like this
<div class="nested-fields">
<%= link_to_remove_association "remove task", f %>
<%= f.inputs :entry %>
<div>
<% f.semantic_form_fields_form :steps do |builder| %>
<%= render 'step_fields' :f => builder %>
<% end %>
<%= link_to_add_association 'add step', f, :steps %>
</div>
</div>
lastly, the _step_fields.html.erb page is
<div class="nested-fields">
<%= link_to_remove_association "remove step", f %>
<%= f.inputs :title %>
</div>
Do you see this in the log?:
WARNING: Can't mass-assign protected attributes: steps_attributes
If so, add this to the Task model:
attr_accessible :steps_attributes

Resources