Rails fields_for form not showing up, nested form
Please try
= f.fields_for :paintings, Painting.new do |p|
Nested form is not showing up!
Why are you doing fields_for ".."
?
What you want to do is <%= f.fields_for :characters %>
as that will iterate through all the characters and render the fields as required in the account form. By calling fields_for
on the f
object you're telling the parent form that it contains nested attributes.
Secondly, you'll need to use the @account
object in your form rather than building another one with @user.accounts.build
. By doing it this wrong way, you're actually creating a new account object which wouldn't have any character objects pre-built for it.
In addition to this you will need to specify accepts_nested_attributes_for :characters
in your Account
model so that the form accepts them along with the account parameters.
Nested forms, using form_with and fields_for, iteration not working
Try setting your collection of klps
to an instance variable that you can then pass as an explicit "record object" argument to the fields_for
method:
apps/controllers/klasses_controller.rb
def new
@klasse = Klasse.new
@klps = 3.times { @klasse.klps.build }
end
app\views\klasses\new.html.erb
<%= form.fields_for :klps, @klps do |klps_form| %>
... your form...
<% end %>
See also: How do I pass an array to fields_for in Rails?
Reference: https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
Rails fields_for form not showing up
Do you want many discounts or one discount?
@product.build_discount
is used in a has_one
association, but the rest of your code is for has_many
If you want many discounts then change it to @product.discounts.build
Otherwise, if you want just one discount, change the following:
f.fields_for :discount do |discount_form|
and accepts_nested_attributes_for :discount
to singular.
@products.discounts.build
won't work because you can't get an association from a collection of objects. For example:
@products = Product.all
@discount = @products.discounts.build
# This won't work! You'll get an error
@product = Product.find(params[:id])
@discount = @product.discounts.build
# This will work, since you're running it on a single instance of Product
Cocoon nested form not showing in Rails 5
Your nested field form is displayed with this code:
<%= f.simple_fields_for :ingredients do |ingredient| %>
<p>ajsd</p>
<%= render 'ingredients_fields', f: ingredient %>
<%= link_to_add_associtation 'Add Ingredient', f, :ingredients %>
<% end %>
The code inside simple_fields_for
is executed once for each ingredient in @recipe (and is not shown if @recipe does not have any ingredients).
I guess you are creating an empty recipe. What you can do is add an empty ingredient to the recipe in the new method:
@recipe.ingredients.build
This will show an empty recipe in the form.
Another important issue is that the link_to_add_association
(which has a typo in your OP) is inside the simple_fields_for
. It must be outside, so that it is shown at the end, and even if no ingredients are present.
Finally, you are also missing:
accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true
Rails Newbie, nested fields_for not showing on edit when there is no existing record
Since the user does not have a dealer_constant at the time of edit, the fields are not showing up. What you need is following:
In user.rb add this method
def with_dealer_constant
self.biuld_dealer_constant if self.dealer_constant.nil?
self
end
Then, in app/views/user/edit.html.erb view file, do this:
<%= form_for([resource_name, resource.with_dealer_constant], url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= f.fields_for :dealer_constant do |builder| %>
<%= render "dealer_constant_fields", :f => builder %>
<% end %>
<!-- rest of the fields -->
<% end %>
Rails - Nested form fields_for not rendering
It seems you do not want to be creating new ivp6_addresses, all you want to do is build the association between computer and address when the computer is created. You can either do this with a third table to define all the computer/ivp6 relationships or you can tweak your model definitions a little bit. Below is how to do it without adding a third table.
The relationship may seem a little backwards in the wording but rails will find the ipv6 association by finding the ivp6_address_id in the computers table. If it was the other way around you would need a new ivp6_address for every computer created which is not what you said you wanted. Make sure you have the appropriate id column in the computers table for the ivp6_address_id or whatever foreign key you want to use.
Computer model:
class Computer < ActiveRecord::Base
belongs_to :ipv6_address
...
Ipv6Address model:
class Ipv6Address < ActiveRecord::Base
attr_accessible :ip_address
has_many :computers
...
Then get rid of the nested fields in your computer form and add the line below to build the association when the form is submitted.
<%= form_for( @computer ) do |f| %>
<!--All the computer fields you need-->
<%= collection_select(:computer, :ipv6_address_ids, Ipv6Address.all, :id, :ip_address, {}, { :multiple => true } )%>
<% f.button :submit %>
<% end %>
Related Topics
How to Redefine a Ruby Constant Without Warning
Ruby: Module, Require and Include
Rails: Testing Named Scopes with Rspec
Setting Up the Logger in Rails 3
Reraise (Same Exception) After Catching an Exception in Ruby
How to Access Url Helper from Rails Module
How to Describe an Enumeration Column in a Rails 3 Migration
Split Ruby Regex Over Multiple Lines
Rails: How to Make Date Strftime Aware of the Default Locale
How to Find the Source File for a Rake Task
Rspec: How to Test File Operations and File Content
Ruby: Create a String from Bytes
Why Is Safe Navigation Better Than Using Try in Rails
What Is the Argument Against Using Before, Let and Subject in Rspec Tests