Rails: Saving Parent and Child simultaneously with nested forms - ruby-on-rails

I have an Event model and a Packages model:
# event.rb
belongs_to :organization, optional: true
belongs_to :event_type, optional: true
has_many :packages
has_many :collaborations
has_many :messages, through: :collaborations
has_many :forms, through: :collaborations
# package.rb
belongs_to :event
has_many :package_collaborations
has_many :collaborations, through: :package_collaborations
I want the user to be able to create an Event and Packages at the same time with a nested form like this:
These are my two forms:
#_event_form.rb (simplified for easier reading)
<%= form_for(event) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<br>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<% if f.object.new_record? %>
<%= f.hidden_field :status, value: 'upcoming' %>
<% else %>
<div class="form-group">
<%= f.label :attendee_count %>
<%= f.number_field :attendee_count, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :status %>
<%= f.select :status, [['Upcoming','upcoming'],['Completed','completed']], {}, class: 'form-control' %>
</div>
<% end %>
<hr>
<div class="form-group required">
<%= f.label :started_at, 'Starts' %>
<div class='input-group date form_datetime'>
<% if event.started_at.present? %>
<%= f.text_field :started_at, class: 'form-control', value: event.started_at.strftime("%m/%d/%Y %H:%M %p") %>
<% end %>
</div>
</div>
<div class="form-group required">
<%= f.label :ended_at, 'Ends' %>
<div class='input-group date form_datetime'>
<% if event.ended_at.present? %>
<%= f.text_field :ended_at, class: 'form-control', value: event.ended_at.strftime("%m/%d/%Y %H:%M %p") %>
<% end %>
</span>
</div>
</div>
<%= f.hidden_field :platform_type, value: "a_platform" %>
<hr>
<%= render 'packages/package_form', event: #event %> # <--- Nested form here.
<div class="actions" id="listen-for-hover">
<%= f.submit 'Save', class: 'btn btn-primary', :id => "event-create-button" %>
</div>
<% end %>
#_package_form.rb
<%= form_for([#event, #package]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :kind, value: params[:kind] if f.object.new_record? %>
<div class="form-group required">
<%= f.label :Your_offer_?, "Your Offer?" %>
<%= f.text_area :offering, class: 'form-control', :placeholder => "e.g. Your logo on stuff" %>
</div>
<div class="form-group">
<%= f.label :notes %>
<%= f.text_area :notes, class: 'form-control', :placeholder => "Notes here" %>
</div>
<hr>
<div class="actions">
<%= f.submit 'Submit', class: 'btn btn-primary', id: 'package-submit-button', disabled: true %>
</div>
<% end %>
Using the form this way results in this error:
Extracted source (around line #3):
1 <%# remoteFlag = false %>
2 <%# remoteFlag = true if #event.title == "dummy event" %>
3 <%= form_for([#event, #package]) do |f| %>
4 <%= render 'shared/error_messages', object: f.object %>
Obviously this is because #event hasn't been created yet so there isn't an ID for it. What would be the best way to make this nested form that is used to make new Event/Packages (new Objects with no ID) at the same time or Edit existing (Existing objects with IDs) ones by using the partial in another part of the app?

You need to add
accepts_nested_attributes_for :package
to your Event model (in event.rb). Then, in the 'new' method of your events controller, add
#package = #event.packages.build
In your form, you will need to use the fields_for method. There is an excellent Railscast on this whole process here: http://railscasts.com/episodes/196-nested-model-form-part-1
If you're not familiar with Ryan Bates' Railscasts, I highly recommend you spend some time with them.

You can use the 'build' method for creating an empty 'package' in your 'event'. There is an example on the official RoR guides
E.g.
<%= form_for([#event, #event.packages.build]) do |f| %>

Related

Pass a value to a Form in rails

I have to pass a value in a form where the user cannot to choose. I have a Register model with some fields. One of them is a value from another Model called Car. I show in the _form view the car plate value but I want to link this value in the form This is the code in _form :
<%= form_for(#reg) do |f| %>
<div class="field">
<p><%= f.label :date %></p><br>
<%= f.date_field :date_reg %>
</div>
<div class="field">
<p><%= f.label :car_id %></p><br>
<% Car.all.each do |car| %>
<%= car.plate %>
<%= f.select(:driver_id,
options_from_collection_for_select(Driver.all, :id,
:name), {:prompt => 'Please Choose'}, :class => "form-
control") %><br>
<% end %>
</div>
<div class="field">
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Thanks all

Extend forms without page reload, rails

I'm searching for a solution to extend my form without page reload.
First I tried to render a partial with coffee or javascript, but escape_javascript didnt work.
Here's the view
<%= form_for #recipe = current_user.recipes.build do |f| %>
<%= f.label :name, "Recipe Name" %>
<%= f.text_field :name %>
<%= button_to "Add Ingredient", '#', class: "btn btn-lg btn-primary", id: "add" %>
<p></p>
<%= f.submit class: "btn" %>
<% end %>
The form above should be extented with following partial by every click on the button
<div id="recipe-ingredients">Quantity
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= render 'quantity_fields', f: quantity %>
<% end %>
</div>
_quantity_fields
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
This approach did not work (recipes.js.erb)
$(document).ready(function() {
$("#add").click(function() {
$("p").append("<%= escape_javascript
render(:partial => 'quantities') %>");
});
});
There's a workaround (see below) but I'm searching for a better solution.
$(document).ready(function() {
$("#workout-week").append(<%= escape_javascript(
Haml::Engine.new(File.read(File.join(Rails.root,'app/views',
'_show_period.html.haml'))).render(Object.new, period: #period)) %>);
});
A second approach is to write following lines in Coffee or JavaScript:
<%= f.fields_for :quantities, #recipe.quantities.build do |quantity| %>
<%= f.label :amount, "Amount:" %>
<%= f.text_field :amount %>
<%= f.collection_select(:ingredient_id, Ingredient.all, :id, :name) %>
<% end %>
I'm a newbie so take this with a grain of salt, but have you tried using render :foo instead of redirect_to :foo in the appropriate controller function?
I solved it with cocoon
<%= form_for #recipe do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.fields_for :quantities do |quantity| %>
<%= render 'quantity_fields', :f => quantity %>
<% end %>
<div class="links">
<%= link_to_add_association 'add', f, :quantities %>
</div>
</div>
<%= f.submit %>
<% end %>

Rails has many through not working

I've got 3 models:
Vacancy
has_many :address_vacancies
has_many :addresses, through: :address_vacancies
Address
has_many :address_vacancies
has_many :vacancies, through: :address_vacancies
AddressVacancy
belongs_to :address
belongs_to :vacancy
and in my form I use the following code:
<%= f.collection_select :address_id, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
which throws an error: undefined method address_id', why is that and what am I doing wrong?
Edit
The full form looks like this:
<%= form_for(#vacancy) do |f| %>
<% if #vacancy.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#vacancy.errors.count, "error") %> prohibited this vacancy from being saved:</h2>
<ul>
<% #vacancy.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.collection_select :address, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
</div>
<div class="field">
<%= f.label :signup_until %><br>
<%= f.date_select :signup_until %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This:
<div class="field">
<%= f.label :address %>
<%= f.collection_select :address, Address.order("CREATED_AT DESC"),:id,:title, include_blank: true %>
</div>
Isn't valid in the context of a Vacancy form (from the form_for #vacancy) since a Vacancy doesn't have a single address attribute. It has a collection of addresses through the has_many association. It would also be invalid to use :address_id for the same reason.
If you want to edit the various addresses for the one vacancy, you would need a subform. You'd use accepts_nested_attributes_for in the model, and fields_for in the view.
<%= form_for #vacancy do |f| %>
...
<%= f.fields_for :addresses do |af| %>
...
<!-- Here you'd render a partial or a set of inputs for this address %>
<%= af.input :street %> <!-- e.g., if there's a street attribute for Address -->
...
<% end %>
...
<% end %>

Rails 4 - Nested Object Won't Save

Note: I've read a couple posts similar to this. But non of the solutions answer my question
I have two objects, Bid and Moz. When I build my Bid object, everything seems to save okay, except for the Moz objects.
Model
class Bid < ActiveRecord::Base
belongs_to :user
has_many :mozs, :dependent => :destroy
accepts_nested_attributes_for :mozs, :allow_destroy => true
end
class Moz < ActiveRecord::Base
belongs_to :bid
end
Bids::Build Controllers
class Bids::BuildController < ApplicationController
include Wicked::Wizard
steps :intro, :problems, :solutions, :pricing
def show
#bid = Bid.find(params[:bid_id])
render_wizard
end
def update
#bid = Bid.find(params[:bid_id])
#bid.attributes = build_params
4.times { #bid.mozs.build } if step == steps.second
render_wizard #bid
end
def new
#bid = Bid.new
redirect_to wizard_path(steps.first, :bid_id => #bid.id)
end
def build_params
params.require(:bid).permit(:client_name, :intro, :prob1, :prob2, :prob3, :site_feel, :search_phrase, :page_score, :total_links,
:internal_links, :external_links, :competition, :complete, :user_id, :us_company, :philosophy_1,
:philosophy_2, :website_conclusions, :is_onsite_seo, :onsite_seo, :is_ongoing_seo, :ongoing_seo,
:is_ppc, :ppc, :is_social_media, :social_media, :is_google_places, :google_places, :is_adwords_express,
:adwords_express, moz_attributes: [:url, :id, :_destroy]
)
end
private
def finish_wizard_path
root_url
end
end
solutions.html.erb
<%= form_for (#bid), url: wizard_path do |f| %>
<% if #bid.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bid.errors.count, "error") %> prohibited this bid from being saved:</h2>
<ul>
<% #bid.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% if #bid.is_onsite_seo? %>
<div class="field">
<%= f.label :onsite_seo %><br>
<%= f.text_area :onsite_seo %>
</div>
<% end %>
<% if #bid.is_ongoing_seo? %>
<div class="field">
<%= f.label :ongoing_seo %><br>
<%= f.text_area :onsite_seo %>
</div>
<% end %>
<div class="field">
<%= f.label :ppc %><br>
<%= f.text_area :ppc %>
</div>
<div class="field">
<%= f.label :social_media %><br>
<%= f.text_area :social_media %>
</div>
<div class="field">
<%= f.label :google_places %><br>
<%= f.text_area :google_places %>
</div>
<div class="field">
<%= f.label :adwords_express %><br>
<%= f.text_area :adwords_express %>
</div>
<%= f.fields_for :mozs do |builder| %>
<%= render partial: "moz_fields", locals: {f: builder} %>
<% end %>
<%= link_to_add_association "Add URL", f, :mozs %>
<div class="actions">
<%= f.submit %>
or <%= link_to "skip this step", next_wizard_path %>
</div>
<% end %>
_moz_fields.html.erb
<div class="field fields">
<%= f.label :url, "Comparative URL" %><br>
<%= f.text_field :url %>
<%= f.hidden_field :destroy %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
I don't understand why they won't save. In addition, I noticed something odd -- when I don't use a partial for the nested object and use the f form builder for the #bid object (as opposed to 'builder'), I get an error no method or variable :url, but a Moz object is saved (although, not with any of the desired attributes).
My opinion that you misspelled with permit attrbibutes hash, try to change moz_attributes to mozs_attributes.
params.require(:bid).permit(..., :mozs_attributes: [:url, :id, :_destroy])
If you send the parameter _destroy: 1 through your hidden field
<%= f.hidden_field :destroy %>
you instruct Rails to destroy the child moz object, or in your case, prevent it from being created.
As for the second part of your question, if you inline the partial from this
<%= f.fields_for :mozs do |builder| %>
<%= render partial: "moz_fields", locals: {f: builder} %>
<% end %>
to this
<%= f.fields_for :mozs do |builder| %>
<div class="field fields">
<%= f.label :url, "Comparative URL" %><br>
<%= f.text_field :url %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
<% end %>
it won't work, because the model object for the scope f is your #bid, not moz. Bids have no url attribute, hence the error.
With the input fields being created in the wrong form builder scope, you did not actually transmit any attributes for your moz object, and so it was created blank. As a side effect, this also meant not sending the _destroy parameter, so the object was saved.
Instead, inline the partial like this (I renamed builder to moz for clarity):
<%= f.fields_for :mozs do |moz| %>
<div class="field fields">
<%= moz.label :url, "Comparative URL" %><br>
<%= moz.text_field :url %>
<%= link_to_function "remove", "remove_fields(this)"%>
</div>
<% end %>

Nested_form_for with field_for not working

my view is
<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for #item_category, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
<div><%= f.label :item_name %>
<%= f.text_field :item_name %></div>
</br>
<div>
<%= f.fields_for :item_companies do |f| %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
</div>
</br>
<%= f.fields_for :item_weights do |f| %>
<div>
<%= f.label :weight %>
<%= f.text_field :weight %>
</div>
</br>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<%end%>
<%end%>
<div><%= f.submit "Submit" %></div>
<% end %>
</div>
and controller
def new
#item_category = ItemCategory.new
item_company = #item_category.item_companies.build
item_company.item_weights.build
end
and when I have applied debugger in create action and I saw value of params[:item_category] and this is generated {"item_name"=>"iname", "item_companies_attributes"=>{"0"=>{"company_name"=>"cname", "item_weights_attributes"=>{"0"=>{"weight"=>"20kg", "price"=>"100"}}}}}
ItemCategory.new(params[:item_category])
generated error
ActiveModel::MassAssignmentSecurity::Error Exception: Can't mass-assign protected attributes: item_companies_attributes.
Where i am wrong and how to save value in three tables using this.
It seems that you forgot to add this piece of code in your ItemCategory model:
attr_accessible :item_companies_attributes
which will allow you to set these attributes.

Resources