Accepting nested attributes on a belongs_to association - ruby-on-rails

I have a complex form for scheduling events. Here are the abbreviated associations:
class Event < ActiveRecord::Base
belongs_to :client
accepts_nested_attributes_for :client, :reject_if => lambda { |a| a[:name].blank? }
end
class Client < ActiveRecord::Base
has_many :events
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }
end
The form is creating a new event and I have the following structure:
- form_for #event do |event_form|
%select=collection_select(client_options_for_select, :options, :group_name, :id, :name, #event.client_id)
- event_form.fields_for :client do |client|
= client.text_field :name
- client.fields_for :questions do |question|
= question.text_field :content
The client already exists and is chosen from a select menu. An observer renders the nested attributes form by setting the client variable in a controller action and then rendering the partial.
Here is the error I'm getting:
ActionView::TemplateError (wrong number of arguments (0 for 1)) on line #1 of app/views/proceedings/_questions.html.haml:
1: - event_form.fields_for :client do |client|
app/views/proceedings/_questions.html.haml:1:in `form'
app/views/proceedings/_questions.html.haml:1:in `_run_haml_app47views47events47_client_questions46html46haml_locals_client_questions_object'
haml (3.0.21) rails/./lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/proceedings_controller.rb:261:in `__instance_exec0'
app/controllers/proceedings_controller.rb:260:in `corp_client_questions'
app/controllers/proceedings_controller.rb:258:in `corp_client_questions'
I'm having problems (I think) with the belongs_to association between Event and Client. I don't know if Event can accept nested attributes of Client when the Event belongs_to the Client. I've always done it the other way around (Client accepts nested attributes of Event).
Any ideas? I can elaborate if you need more code or background. Thanks!
Update: Added controller code as requested.
def client_questions
if params[:client_id].blank?
render_no_client_questions
elsif #client = Client.find(params[:client_id]) and #client.is_unspecified?
render_no_client_questions
else
respond_to do |format|
format.js {
render :update do |page|
page[:client_questions].replace_html :partial => 'client_questions', :layout => false
end
}
end
end
end

try adding an instance of the fields_for object in the options... generally a symbol isn't enough when creating a new top-level form object... Try the following, but yes it is possible to accept nested attributes on a belongs_to.
<%= event_form.fields_for :client, #client do |client| %>
<%= client.text_field :name %>
<%= client.fields_for :questions, Question.new do |question| %>
<%= question.text_field :content %>
<% end %>
<% end %>

I had the same issue using accepts_nested_attributes_for :address on a belongs_to :address association for my Order object.
The field_for wasn't echoing nothing, but all it took was adding
#order.build_address()
and that made it work.
I think is because we are using the association sort of reversed from the usual, so you have to manually create the association.

In your controller method(s), you need to add:
#event.build_client
The form cannot display fields_for when it does not have a valid object. Most of the time, we use something like #event.client.build, but this will not work with a belongs_to association. That method is only valid with has_many and has_and_belongs_to_many.
Reference here.

Related

Rails has_many relationship with prefilled views

I have a pretty basic Rails 4 app, and am using Cocoon's nested forms to manage the has_many... :through model association.
class Student < ActiveRecord::Base
has_many :evaluations
has_many :assessments, through: :evaluations
# ... etc
end
class Evaluation < ActiveRecord::Base
belongs_to :student
belongs_to :assessment
# ... etc
end
class Assessment < ActiveRecord::Base
has_many :evaluations
has_many :students, through: :evaluations
accepts_nested_attributes_for :evaluation, reject_if: :all_blank
# ... etc
end
When I use Cocoon in the View, I want to use the New Assessment view to pre-fill all the Student records in order to create a new Evaluation for each one. I don't want to have to do some hacky logic on the controller side to add some new records manually, so how would I structure the incoming request? With Cocoon I see that requests have some number in the space where the id would go (I've replaced these with ?? below).
{"utf8"=>"✓", "authenticity_token"=>"whatever", "assessment"=>{"description"=>"quiz 3", "date(3i)"=>"24", "date(2i)"=>"10", "date(1i)"=>"2015", "assessments_attributes"=>{"??"=>{"student_id"=>"2", "grade" => "A"}, "??"=>{"student_id"=>"1", "grade" => "B"}, "??"=>{"student_id"=>"3", "grade"=>"C"}}, }}, "commit"=>"Create Assessment"}
I see in the Coccoon source code that this is somehow generated but I can't figure out how it works with the Rails engine to make this into a new record without an ID.
What algorithm should I use (or rules should I follow) to fill in the id above to make a new record?
"??"
Never a good sign in your params.
With Cocoon I see that requests have some number in the space where the id would go
That ID is nothing more than the next ID in the fields_for array that Rails creates. It's not your record's id (more explained below).
From your setup, here's what I'd do:
#app/models/student.rb
class Student < ActiveRecord::Base
has_many :evaluations
has_many :assessments, through: :evaluations
end
#app/models/evaluation.rb
class Evaluation < ActiveRecord::Base
belongs_to :student
belongs_to :assessment
end
#app/models/assessment.rb
class Assessment < ActiveRecord::Base
has_many :evaluations
has_many :students, through: :evaluations
accepts_nested_attributes_for :evaluations, reject_if: :all_blank
end
This will allow you to do the following:
#app/controllers/assessments_controller.rb
class AssessmentsController < ApplicationController
def new
#assessment = Assessment.new
#students = Student.all
#students.each do
#assessment.evaluations.build
end
end
end
Allowing you:
#app/views/assessments/new.html.erb
<%= form_for #assessment do |f| %>
<%= f.fields_for :evaluations, #students do |e| %>
<%= e.hidden_field :student_id %>
<%= e.text_field :grade %>
<% end %>
<%= f.submit %>
<% end %>
As far as I can tell, this will provide the functionality you need.
Remember that each evaluation can connect with existing students, meaning that if you pull #students = Student.all, it will populate the fields_for accordingly.
If you wanted to add new students through your form, it's a slightly different ballgame.
Cocoon
You should also be clear about the role of Cocoon.
You seem like an experienced dev so I'll cut to the chase - Cocoon is front-end, what you're asking is back-end.
Specifically, Cocoon is meant to give you the ability to add a number of fields_for associated fields to a form. This was discussed in this Railscast...
Technically, Cocoon is just a way to create new fields_for records for a form. It's only required if you want to dynamically "add" fields (the RailsCast will tell you more).
Thus, if you wanted to just have a "static" array of associative data fields (which is I think what you're asking), you'll be able to use fields_for as submitted in both Max and my answers.
Thanks to #rich-peck I was able to figure out exactly what I wanted to do. I'm leaving his answer as accepted because it was basically how I got to my own. :)
assessments/new.html.haml (just raw, no fancy formatting)
= form_for #assessment do |f|
= f.fields_for :evaluations do |ff|
.meaningless-div
= ff.object.student.name
= ff.hidden_field :student_id, value: ff.object.student_id
= ff.label :comment
= ff.text_field :comment
%br/
assessments_controller.rb
def new
#assessment = Assessment.new
#students = Student.all
#students.each do |student|
#assessment.evaluations.build(student: student)
end
end

Create parent model from child controller

I'm working on on a web app that takes in information about companies. Information can be taken for a PreferredOffering (of stock) or an Incorporation. So in other words, when I create a new entry to either one of those models, a new company is formed.
It works out that my database is cleaner if PreferredOffering and Incorporation are children of Company, even though I'm trying to go through the preferred_offerings_controller or theincorporations_controller to create a new Company. Here lies my question; I'm trying to figure out how to configure my view and controllers to create a parent model from a child controller. I've done some research and have seen two other S/O posts on how to accomplish this with Rails 3, however it would seem that the addition of strong params adds another layer of complexity to the endeavor.
So I have my models set up like this
class Company < ActiveRecord::Base
belongs_to :user
has_one :incorporation, dependent: :destroy
has_many :preferred_offerings, dependent: :destroy
accepts_nested_attributes_for :preferred_offerings, allow_destroy: true
accepts_nested_attributes_for :incorporation, allow_destroy: true
end
.
class Incorporation < ActiveRecord::Base
belongs_to :company
end
.
class PreferredOffering < ActiveRecord::Base
belongs_to :company
end
The controller and view are what I'm iffy on.
Let's just take a look at the incorporation view/controller. If I were to configure it so that Incorporation has_one :company, I would set it up as follows:
class IncorporationsController < ApplicationController
def index
end
def new
#user=current_user
#incorporation = #user.incorporations.build
#company = #incorporation.build_company
end
def create
#incorporation = current_user.incorporations.build(incorporation_params)
end
private
def incorporation_params
params.require(:incorporation).permit(:title, :trademark_search, :user_id, :employee_stock_options, :submit, :_destroy,
company_attributes: [:id, :name, :employee_stock_options, :options_pool, :state_corp, :street, :city, :state, :zip, :issued_common_stock, :outstanding_common_stock, :fiscal_year_end_month, :fiscal_year_end_day, :user_id, :_destroy]
)
end
end
And the view would be:
<%= simple_form_for #incorporation, html: {id:"incorporationform"}, remote: false, update: { success: "response", failure: "error"} do |f| %>
(incorporation-specific fields)
<%= f.simple_fields_for :company do |company| %>
(Company-specific fields)
<% end %>
<% end %>
So my question is:
How do I need to modify my controller and view to create a Company from the incorporations_controller IF Company has_one :incorporation
Any suggestions would be much appreciated.
While it isn't the "Rails Way", there is nothing really wrong with having #company being the parent in your form, even though it is in the incorporations#new action. Your view would change to this:
<%= simple_form_for #company, html: {id:"companyform"}, remote: false, update: { success: "response", failure: "error"} do |f| %>
(company-specific fields)
<%= f.simple_fields_for :incorporation do |incorporation| %>
(incorporation-specific fields)
<% end %>
<% end %>
And your strong params would change so that Company is the parent and Incorporation is the child.
Another option would be to simply go through the Company controller. You could create two new actions: new_preferred_offering and new_incorporation. You would then create the objects in those actions. Or you could pass in some kind of :type param so that the normal new action renders one of two forms based on which one you want.

How do I store a record once and reference it many times in other tables?

I have a table of ingredients. When a user inputs an ingredient, it saves in the table. Now what I'm noticing is that many records repeat, and I don't want that, what I want is for each of these records to be referenced if it already exists.
So for example, I have flour saved about 20 times in the DB. I want it to be saved once, and for each recipe, when a user inputs flour, it should just reference that ingredient in the DB. How do I achieve this?
I already have an Entry model (recipe), and a join EntryIngredient model. So whenever an Entry saves an Ingredient that already exists, I want it to just reference that one in the EntryIngredient table (as foreign key) instead of create a new one.
class Ingredient < ActiveRecord::Base
has_many :entry_ingredients
has_many :entries, :through => :entry_ingredients
end
class EntryIngredient < ActiveRecord::Base
belongs_to :entry
belongs_to :ingredient
accepts_nested_attributes_for :ingredient, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end
class Entry < ActiveRecord::Base
belongs_to :user
has_many :entry_ingredients
has_many :ingredients, :through => :entry_ingredients
has_many :steps
accepts_nested_attributes_for :entry_ingredients, :allow_destroy => true
accepts_nested_attributes_for :steps, :reject_if => lambda { |s| s[:description].blank? }, :allow_destroy => true
end
UPDATE:
Dynamic finders sound like they do what I want, specifically find_or_create or first_or_create but I'm not able to figure out how to use them in my setup. Can anyone push me in the right direction here? This is what I've tried but I realize that this is just creating a new ingredient whenever the entries#new is called... not what I want to happen
#entries_controller.rb
def new
#entry = Entry.new
#entry.entry_ingredients.build.build_ingredient
#entry.steps.build
#ingredient = Ingredient.where(params[:ingredient]).first_or_create()
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #entry }
end
end
#_entry_ingredient_fields.html.erb
<%= f.fields_for :ingredient do |builder| %>
<%= builder.label :name %>
<%= builder.text_field :name, :class => "ingredient_field" %>
<% end %>
It's hard to know exactly what you need without seeing some code, but if I understand correctly, you want to create an ingredient only if it doesn't exist yet.
Check out rails' dynamic finders. If you use a method name like
#ingredient = Ingredient.find_or_create_by_name("flour")
it will create a new object if there was no entry with the name "flour" before or return an existing ingredient called "flour". In both cases, #ingredient will hold the desired entry returned by the statement above. Rails will create it for you only if it doesn't exist yet.

Multi check_box on array field of child resource creates new items

I am trying to create multiple checkbox tags for second level embedded documents. However, now it just creates new objects.
I have models
class Paprasa
include Mongoid::Document
attr_accessible :id, :bkompetencijas, :bkompetencijas_attributes
accepts_nested_attributes_for :etapa
accepts_nested_attributes_for :bkompetencijas, :allow_destroy => true
embeds_many :bkompetencijas , class_name: 'Kompetencija', inverse_of: :paprasabk, :cascade_callbacks => true
end
#-----------------------------------------------
class Kompetencija
include Mongoid::Document
attr_accessible :id, :paprasabk, :paprasabk_attributes, :siekinys, :siekinys_attributes
accepts_nested_attributes_for :paprasa, :allow_destroy => true
accepts_nested_attributes_for :siekinys, :allow_destroy => true
embedded_in :paprasabk, class_name: 'Paprasa', inverse_of: :bkompetencijas
embeds_many :siekinys, class_name: 'Siekiny' , inverse_of: :kompetencija, :cascade_callbacks => true
end
#-----------------------------------------------
class Siekiny
include Mongoid::Document
field :matricos_ids, :type => Array, :default => []
attr_accessible :id, :kompetencija, :kompetencija_attributes, :matricos_ids
accepts_nested_attributes_for :kompetencija
embedded_in :kompetencija , class_name: 'Kompetencija', inverse_of: :siekinys
end
a form
<%= form_for [#paprasa],:html => { :multipart => true} do |f| %>
<%= f.fields_for :dkompetencijas do |form_inner| %>
<%= form_inner.fields_for :siekinys do |form_inner_inner| %>
<% #some_other_object.each_with_index do |d,indexx| %>
<%= check_box_tag "#{field_name_for_js(form_inner_inner, "matricos_ids")}[]", d.id, form_inner_inner.object.matricos_ids.include?(d.id), :id => "#{field_id_for_js(form_inner_inner, "matricos_id_") << d.id.to_s}" ).to_s %>
<% end %>
<% end %>
<% end %>
<% end %>
where
def field_id_for_js(builder, attribute)
"#{builder.object_name}[#{attribute.to_s.sub(/\?$/,"")}]".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end
def field_name_for_js(builder, attribute)
"#{builder.object_name}[#{attribute.to_s.sub(/\?$/,"")}]"
end
it gives me ouput like this
<input id="paprasa_dkompetencijas_attributes_0_siekinys_attributes_0_matricos_id_506567916226f718e50000cb" name="paprasa[dkompetencijas_attributes][0][siekinys_attributes][0][matricos_ids][]" type="checkbox" value="506567916226f718e50000cb" />
<input id="paprasa_dkompetencijas_attributes_0_siekinys_attributes_1_matricos_id_506567916226f718e50000cb" name="paprasa[dkompetencijas_attributes][0][siekinys_attributes][1][matricos_ids][]" type="checkbox" value="506567916226f718e50000cb" />
and parameters
{"paprasa"=>{"dkompetencijas_attributes"=>{"0"=>{"siekinys_attributes"=>{"0"=>{"id"=>"505adfd26226f7555a000191", "matricos_ids"=>["506567916226f718e50000cb"]}, "1"=>{"id"=>"507558176226f75fa5000033"}, "2"=>{"id"=>"512f19626226f765c7000071"}, "3"=>{"id"=>"512f36456226f765c70000c8"}}, "id"=>"505adfd26226f7555a000190"}}}, "user_id"=>"5058514b6226f73ae4000064", "etapa_id"=>"505851516226f73ae4000065"}
this just creates new "siekiny" objects instead of updating existing ones. What I am doing wrong?
I actually ran into this problem recently. Accepts_nested_attributes_for cannot(yet) decipher with a new parent object form whether to create new child objects or find and update existing ones. If you don't want your child objects being duplicated when you create a new parent object then you will have to create a custom setter method to handle the find_or_create for you.
Checkout THIS question which solves the problem by overriding autosave associations.
Or, checkout THIS question which solves the problem by using reject_if and a custom method.
Another option to solve this problem is to break down the hash in the controller and do all object creating there instead of in the model.
However, this did not work for me. For some actions it worked several times and then broke again. I even tried from #some_other_object side (this object is referenced from paprasa and it just kept creating new objects. The only way I got this was putting some "hackish" things in controller, which I really don't like.
I took parent object, iterated through child using index on params to look for specified array field and assigned to child attribute, then the .save method. Now everything works, but i do not Like it...

Nested form in active_admin with select or create option

We are using active_admin for our administration backend.
We have a model "App" that :belongs_to model "Publisher":
class App < ActiveRecord::Base
belongs_to :publisher
end
class Publisher < ActiveRecord::Base
has_many :apps
end
When creating a new entry for the "App" model I want to have the option to either select an existing publisher or (if the publisher is not yet created) to create a new publisher in the same (nested) form (or at least without leaving the page).
Is there a way to do this in active_admin?
Here's what we have so far (in admin/app.rb):
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :title
...
end
f.inputs do
f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
p.input :name
end
end
f.buttons
end
After hours of searching, I'd appreciate any hint... Thanks!
First, make sure that in your Publisher model you have the right permissions for the associated object:
class App < ActiveRecord::Base
attr_accessible :publisher_attributes
belongs_to :publisher
accepts_nested_attributes_for :publisher, reject_if: :all_blank
end
Then in your ActiveAdmin file:
form do |f|
f.inputs do
f.input :title
# ...
end
f.inputs do
# Output the collection to select from the existing publishers
f.input :publisher # It's that simple :)
# Then the form to create a new one
f.object.publisher.build # Needed to create the new instance
f.semantic_fields_for :publisher do |p|
p.input :name
end
end
f.buttons
end
I'm using a slightly different setup in my app (a has_and_belongs_to_many relationship instead), but I managed to get it working for me. Let me know if this code outputs any errors.
The form_builder class supports a method called has_many.
f.inputs do
f.has_many :publisher do |p|
p.input :name
end
end
That should do the job.
Update: I re-read your question and this only allows to add a new publisher, I am not sure how to have a select or create though.
According to ActiveAdmin: http://activeadmin.info/docs/5-forms.html
You just need to do as below:
f.input :publisher
I've found you need to do 3 things.
Add semantic fields for the form
f.semantic_fields_for :publisher do |j|
j.input :name
end
Add a nested_belongs_to statement to the controller
controller do
nested_belongs_to :publisher, optional: true
end
Update your permitted parameters on the controller to accept the parameters, using the keyword attributes
permit_params publisher_attributes:[:id, :name]

Resources