Validation errors not showing up - Rails 4 - ruby-on-rails

For some reason validation errors are not showing up.
my form
<%= form_for [#question.category, #question] do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field panel">
<%= f.label :question_type %><br>
<%= f.select :question_type, [ ["single","single"],["multiple","multiple"] ], selected: f.object.question_type %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field panel">
<%= f.label :image %><br>
<% if #question.image? %>
<div class="explanation-image text-center">
<%= image_tag #question.image_url(:resized) %>
<p>
<label>
<%= f.check_box :remove_image %>
Remove image
</label>
</p>
</div>
<% end %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field panel">
<%= f.label :explanation %><br>
<%= f.text_area :explanation, size: "30x10" %>
</div>
<div class="field panel">
<%= f.label :link_name %><br>
<%= f.text_field :link_name %>
</div>
<div class="field panel">
<%= f.label :link_url %><br>
<%= f.text_area :link %>
</div>
<div class="field panel">
<%= f.label :video_url %><br>
<%= f.text_area :video_url %>
</div>
<div class="field panel">
<%= f.label :category_id %><br><%= #category.title %>
<%= f.hidden_field :category_id %>
</div>
<div class="actions">
<br>
<%= f.submit 'Submit', class:"button round success" %> <%= link_to 'Back', category_questions_path, class: "button round alert" %>
</div>
<% end %>
this is the model
class Question < ActiveRecord::Base
belongs_to :category
has_many :choices
mount_uploader :image, ImageUploader
validates :description, length: {
minimum: 6
}
validates :link, presence: true
end
and parts of the controller
def edit
#category = Category.find(params[:category_id])
#question = #category.questions.find_by(id: params[:id])
end
def update
respond_to do |format|
if #question.update(question_params)
format.html { redirect_to category_question_url(#question.category, #question), notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: #question }
else
format.html {
#category = Category.find(params[:category_id])
#question = #category.questions.find_by(id: params[:id])
render action: :edit
}
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
the error messages code is directly from the scaffolding. i haven't touched it. if i try to edit a question and save it while lets say link field is empty it will reload the edit action correctly but no error message will pop up.
Any clues?

If it fails to save, you redefine the #question variable, before rendering out the page.
#question = #category.questions.find_by(id: params[:id])
this will delete the object which failed to save and was holding the validation errors, and replace it with the one loaded out of the database. Don't do this. I think if you just delete this line it might work ok.

Related

undefined method from show.html.erb in rails cocoon nested form

I have created a nested form via the cocoon gem, and all is working nicely; however, after filling out the nested form, I can't figure out how to display the nested form data after updating the form in the show.html.erb.
My code:
developments_controller.rb
class DevelopmentsController < ApplicationController
before_action :set_development, only: %i[ show edit update destroy ]
# GET /developments or /developments.json
def index
#developments = Development.all
end
# GET /developments/1 or /developments/1.json
def show
end
# GET /developments/new
def new
#development = Development.new
end
# GET /developments/1/edit
def edit
end
# POST /developments or /developments.json
def create
#development = Development.new(development_params)
respond_to do |format|
if #development.save
format.html { redirect_to #development, notice: "Development was successfully created." }
format.json { render :show, status: :created, location: #development }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #development.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /developments/1 or /developments/1.json
def update
respond_to do |format|
if #development.update(development_params)
format.html { redirect_to #development, notice: "Development was successfully updated." }
format.json { render :show, status: :ok, location: #development }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #development.errors, status: :unprocessable_entity }
end
end
end
# DELETE /developments/1 or /developments/1.json
def destroy
#development.destroy
respond_to do |format|
format.html { redirect_to developments_url, notice: "Development was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_development
#development = Development.find(params[:id])
end
# Only allow a list of trusted parameters through.
def development_params
params.require(:development).permit(:name, :dev_type, :address, :description, :completion, :body_corp, listings_attributes: [:id, :status, :lot_number, :price, :listing_type, :bed, :bath, :car, :land_size, :house_size, :rent, :done, :_destroy])
end
end
development.rb (parent model)
class Development < ApplicationRecord
has_many :listings, inverse_of: :development
accepts_nested_attributes_for :listings, reject_if: :all_blank, allow_destroy: :true
end
listing.rb (child model)
class Listing < ApplicationRecord
belongs_to :development
end
_form.html.erb
<%= form_for #development do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :dev_type %>
<br/>
<%= f.text_field :dev_type %>
</div>
<div class="field">
<%= f.label :address %>
<br/>
<%= f.text_field :address %>
</div>
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :completion %>
<br/>
<%= f.text_field :completion %>
</div>
<div class="field">
<%= f.label :body_corp %>
<br/>
<%= f.text_field :body_corp %>
</div>
<h3>Tasks</h3>
<div id="listings">
<%= f.fields_for :listings do |listing| %>
<%= render 'listing_fields', f: listing %>
<% end %>
<div class="links">
<%= link_to_add_association 'add listing', f, :listings %>
</div>
</div>
<%= f.submit %>
<% end %>
_listing_fields.html.erb
<%= form_for #listing do |f| %>
<h3>New Listing</h3>
<div class="nested-fields">
<div class="field">
<%= f.label :status %>
<br/>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :lot_number %>
<br/>
<%= f.text_field :lot_number %>
</div>
<div class="field">
<%= f.label :price %>
<br/>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :listing_type %>
<br/>
<%= f.text_field :listing_type %>
</div>
<div class="field">
<%= f.label :bed %>
<br/>
<%= f.text_field :bed %>
</div>
<div class="field">
<%= f.label :bath %>
<br/>
<%= f.text_field :bath %>
</div>
<div class="field">
<%= f.label :car %>
<br/>
<%= f.text_field :car %>
</div>
<div class="field">
<%= f.label :land_size %>
<br/>
<%= f.text_field :land_size %>
</div>
<div class="field">
<%= f.label :house_size %>
<br/>
<%= f.text_field :house_size %>
</div>
<div class="field">
<%= f.label :rent %>
<br/>
<%= f.text_field :rent %>
</div>
<%= link_to_remove_association "remove listing", f %>
</div>
<%= f.submit %>
<% end %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #development.name %>
</p>
<p>
<strong>Dev type:</strong>
<%= #development.dev_type %>
</p>
<p>
<strong>Address:</strong>
<%= #development.address %>
</p>
<p>
<strong>Description:</strong>
<%= #development.description %>
</p>
<p>
<strong>Completion:</strong>
<%= #development.completion %>
</p>
<p>
<strong>Body corp:</strong>
<%= #development.body_corp %>
</p>
<%= link_to 'Edit', edit_development_path(#development) %> |
<%= link_to 'Back', developments_path %>
I've tried calling on a few variations of the code on what's already in show.html.erb, such as #development.listings, #listings and #development.listings_attributes, but whenever I call on a listings_attribute: such as :price, :status etc. from the params in the developments_controller.rb I continually get an error undefined method error. I can't figure out where I'm going wrong, but I know the data is being stored, as it's present when I go to the /edit page on my localhost. I've been following the setup tutorial on https://github.com/nathanvda/cocoon and from this YouTube video: https://www.youtube.com/watch?v=R2iw5BAKBNA which have been great up until displaying the nested form data! The parent data displays no problems at all.
Any help is greatly appreciated!
You can't call the status method on #development.listings, a collection. You should iterate over the collection and call the method on the Listing instance instead.
<% #development.listings.each do |listing| %>
<%= listing.status %>
<% end %>

money-rails gem not allowing me to input cents into textbox

I am having problems with saving values to my database using the money-rails gem. Whenever I try and input a value with cents into the textbox (i.e. 20.22) it gives me an error saying "Please enter a valid value.The two nearest values are 20 and 21."
Model:
class VideoGame < ActiveRecord::Base
monetize :loose_price_cents, with_model_currency: :price_currency
monetize :cib_price_cents, with_model_currency: :price_currency
monetize :new_price_cents, with_model_currency: :price_currency
end
_form.html.erb:
<%= form_for(#video_game) do |f| %>
<% if #video_game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video_game.errors.count, "error") %> prohibited this video_game from being saved:</h2>
<ul>
<% #video_game.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :game %><br>
<%= f.text_field :game %>
</div>
<div class="field">
<%= f.label :loose_price %><br>
<%= f.number_field :loose_price %>
</div>
<div class="field">
<%= f.label :cib_price %><br>
<%= f.number_field :cib_price %>
</div>
<div class="field">
<%= f.label :new_price %><br>
<%= f.number_field :new_price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
controller update function:
def update
respond_to do |format|
if #video_game.update(video_game_params)
format.html { redirect_to #video_game, notice: 'Video game was successfully updated.' }
format.json { render :show, status: :ok, location: #video_game }
else
format.html { render :edit }
format.json { render json: #video_game.errors, status: :unprocessable_entity }
end
end
end
I have gone into the rails console and changed values around manually, but it seems to base the error off of the database value (db value = 2011 cents, error = "Please enter a valid value.The two nearest values are 20.11 and 21.11.").
Any advice would be appreciated!
Edit:
I figured it out. By adding "step: 0.01" to the end of the number_field method it allows you to increase the value by .01 instead of the 1 by default. It should look like this instead:
<%= form_for(#video_game) do |f| %>
<% if #video_game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video_game.errors.count, "error") %> prohibited this video_game from being saved:</h2>
<ul>
<% #video_game.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :game %><br>
<%= f.text_field :game %>
</div>
<div class="field">
<%= f.label :loose_price %><br>
<%= f.number_field :loose_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :cib_price %><br>
<%= f.number_field :cib_price, step: 0.01 %>
</div>
<div class="field">
<%= f.label :new_price %><br>
<%= f.number_field :new_price, step: 0.01 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Rails form fields passing old data to whitelisted params

UPDATE: Fixed, thanks for pointing it out kjmagic13. You can see the solution below.
So I've been working on this one since yesterday. I have a Rails project with a form_for collecting client data like name, age, etc. There's an "Add spouse?" button which uses javascript to reveal additional spouse input fields. All of these attributes are whitelisted, and the spouse data are actually no more than attributes on the client model (no spouse object).
The FIRST time you input data to these fields, they all pass through fine and get saved. Then when you revisit the page, the form is prepopulated with that data. You can edit the fields and update the client info, but NOT THE SPOUSE FIELDS --> even though those fields are whitelisted and are nothing more than client attributes. I can't even imagine how I'd make this happen on purpose, much less figure out how to fix it.
The form gets submitted, and the params passed in are always the old spouse data, not what went into the input field. However all the new client info IS updated in the same form. Looking in the logs always shows the old spouse data being passed through, as if I never typed anything
Here's the relevant snippets:
in the clients_controller
def update
if #client.report_ready?
outdate_report
end
respond_to do |format|
if #client.update(client_params)
format.html { redirect_to #client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: #client }
else
format.html { render :show }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
def client_params
params.require(:client).permit(:first_name, :last_name, :email, :dob, :address, :city, :state, :zip, :phone, :retire_age, :ss_age, :user_id, :spouse_dob, :spouse_retire_age, :spouse_ss_age, :spouse_first_name, :spouse_last_name)
end
# the form in the view
<%= form_for (#client) do |f| %>
<% if #client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#client.errors.count, "error") %> prohibited this plan from being saved:</h2>
<ul>
<% #client.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<div>
<%= f.label :first_name, class: 'left' %><br>
<%= f.text_field :first_name %>
</div>
<div>
<%= f.label :last_name, class: 'left' %><br>
<%= f.text_field :last_name %>
</div>
<div>
<%= f.label :email, class: 'left' %><br>
<%= f.email_field :email %>
</div>
<div>
<%= f.label :phone, class: 'left' %><br>
<%= f.phone_field :phone %>
</div>
<div>
<%= f.label :dob, 'Birthdate', class: 'left' %>
<%= f.date_field :dob, style: "font-size: .9em;" %>
</div>
<div>
<% if #client.dob.present? %>
<%= f.label :age, class: 'left' %><br>
<%= text_field_tag :age, #client.age, readonly: true %>
<% else %>
<%= hidden_field :age, value: nil %>
<% end %>
</div>
<div>
<%= f.label :retire_age, 'Ret. age', class: 'left' %><br>
<%= f.number_field :retire_age %>
</div>
<div>
<%= f.label :ss_age, 'S.S. age', class: 'left' %><br>
<%= f.number_field :ss_age %>
</div>
<div>
<%= f.submit "Save", class: "button radius tiny" %>
</div>
<% if !#client.new_record? && #client.has_spouse? %>
<%= render 'spouse_fields', f: f %>
<% else %>
<%= render 'spouse_button' %>
<% end %>
<span id="spouse_fields" style="display:none">
<%= render 'spouse_fields', f: f %>
</span>
# the spouse fields (which are really just a continuation of the form)
<hr>
<h1>Spouse Information</h1>
<div>
<%= f.label :spouse_first_name, 'First name', class: 'left' %><br>
<%= f.text_field :spouse_first_name %>
</div>
<div>
<%= f.label :spouse_last_name, 'Last name', class: 'left' %><br>
<%= f.text_field :spouse_last_name %>
</div>
<div>
<%= f.label :spouse_dob, 'Date of Birth', class: 'left' %><br>
<%= f.date_field :spouse_dob, style: "font-size: .9em;" %>
</div>
<div>
<% if #client.spouse_dob.present? %>
<%= f.label :spouse_age, 'Age', class: 'left' %><br>
<%= text_field_tag :spouse_age, #client.spouse_age, readonly: true%>
<% else %>
<%= hidden_field :spouse_age, value: nil %>
<% end %>
</div>
<div>
<%= f.label :spouse_retire_age, 'Ret. age', class: 'left' %><br>
<%= f.number_field :spouse_retire_age %>
</div>
<div>
<%= f.label :spouse_ss_age, 'S.S. age', class: 'left' %><br>
<%= f.number_field :spouse_ss_age %>
</div>
<div>
<%= f.submit "Save", class: "button radius tiny" %>
</div>
# the js for the 'Add spouse button' (just reveals fields, then disappears)
<text id="spouse_button" onclick="toggleFields()">
Add Spouse?
</text>
<script>
function toggleFields() {
document.getElementById("spouse_fields").style.cssText = "";
document.getElementById("spouse_button").style.cssText = "display:none";
};
</script>
FIX:
All I had missed was the location of the hidden extra spouse fields. They needed to be moved into the if/else block above it, like this (but kjmagic13's 2-line solution is clearly better):
<% if !#client.new_record? && #client.has_spouse? %>
<%= render 'spouse_fields', f: f %>
<% else %>
<%= render 'spouse_button' %>
<span id="spouse_fields" style="display:none">
<%= render 'spouse_fields', f: f %>
</span>
<% end %>
You're duplicating the spouse_fields partial when editing the record. There will be two elements with the ID of spouse_fields and the Javascript will only toggle the first one it comes across. The other will remain hidden and is most likely the fields being passed to the server.
<% if !#client.new_record? && #client.has_spouse? %>
<%= render 'spouse_fields', f: f # renders here on edit %>
<% else %>
<%= render 'spouse_button' %>
<% end %>
<span id="spouse_fields" style="display:none">
<%= render 'spouse_fields', f: f # and renders here on edit %>
</span>
You'll want something more along the lines of this:
<%= render 'spouse_button' unless #client.has_spouse? %>
<%= content_tag :span, render('spouse_fields', f: f), style: ( #client.has_spouse? ? nil : 'display:none' ) -%>

ActiveModel Forbidden Error in Rails 4.0.0

Im working on a nested attribute form.
these are the two models..
Employee.rb
class Employee < ActiveRecord::Base
has_one :employee_info, :primary_key => :employeeID, foreign_key: :employeeID
accepts_nested_attributes_for :employee_info
end
EmployeeInfo.rb
class EmployeeInfo < ActiveRecord::Base
belongs_to :employee, primary_key: :employeeID, foreign_key: :employeeID
validates_uniqueness_of :employeeID
end
And i have my form in _form.html.rb
<%= form_for #employee, html: {class: "form form-horizontal validate-form", novalidate: "novalidate"} do |f| %>
<% if #employee.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger alert-dismissable">
<a class="close" data-dismiss="alert" href="#">×</a>
<h2><%= pluralize(#employee.errors.count, "error") %> prohibited this shop from being saved:</h2>
<ul>
<% #employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class='form-group'>
<%= f.label :employeeID, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= f.text_field :employeeID, {class: 'form-control'} %>
</div>
</div>
<div class='form-group'>
<%= f.label :employee_name, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= f.text_field :employee_name, class: 'form-control' %>
</div>
</div>
<%= f.fields_for :employee_info do |ff| %>
<div class='form-group'>
<%= ff.label :hired, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= ff.text_field :hire_date, class: 'form-control' %>
</div>
</div>
<div class='form-group'>
<%= ff.label :terminated, class: 'col-md-2 control-label' %>
<div class='col-md-5'>
<%= ff.text_field :term_date, class: 'form-control' %>
</div>
</div>
<% end %>
<div class='form-actions form-actions-padding-sm'>
<div class='row'>
<div class='col-md-10 col-md-offset-2'><i class='icon-save custom-icon'></i>
<% if params[:action] == "new" %>
<%= f.submit "Create", class: 'btn btn-primary custom-button' %>
<% else %>
<%= f.submit "Update", class: 'btn btn-primary custom-button' %>
<% end %>
<%= link_to 'Cancel', shops_path, class: 'btn' %>
</div>
</div>
</div>
<% end %>
and the update method in controller
def update
respond_to do |format|
p "------------------------------"
p employee_params
if #employee.update(employee_params)
format.html { redirect_to #employee, notice: 'Employee was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
where that p employee_params output in console is
{"employeeID"=>"103", "employee_name"=>"James Michule", "employee_info_attributes"=>{"hire_date"=>"1996-03-12 11:30:00 UTC", "term_date"=>"1996-03-12 11:30:00 UTC", "hourly_rate"=>"7.4", "address"=>"108 E. Jay", "phone_number1"=>"", "phone_number2"=>"", "zipcode"=>"65721", "state"=>"MO", "city"=>"Ozark", "id"=>"30"}}
and when i try to update i get an error..
error:
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
app/controllers/employees_controller.rb:56:in `block in update'
app/controllers/employees_controller.rb:53:in `update'
What is wrong.? Please help
Rails 4 has features from the strong_parameters
so you can use
#employee.update_attributes(params[:employee], permit[:employee_attribute]
or you can do in following way
#employee.update_attributes(params[:employee].permit(:employeeID))
Your employees_controller
private
def employee_params
params.require(:employee).permit(:term_date, :hire_date)
end
Add term_date & hire_date in the employee_params method like the code above.
Hope it solves the issue.
P.S. And please read the topic "Rails Strong parameters" before creating the app on rails >= 4

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

Resources