I have a rails app where I am trying to upload multiple images using nested_attributes but am running into great difficulty. The images don't seem to be saving to mongodb. I have followed this tutorial (though it seems lacking in detail): http://initializd.com/blog/2013/3/upload-multiple-files-with-html5-rails-mongoid-paperclip-and-google-cloud-storage
Basically, I have a form where a user is able to submit multiple listings (as nested attributes), and under each listing they are able to submit multiple images (also as nested attributes).
Here is my form:
The important part is the file_field_tag where the user is supposed to upload multiple images.
<%= nested_form_for #user, url: wizard_path, :html => { :multipart => true } do |f| %>
<h1 style="margin-bottom: 6%;"> Add Listings </h1>
<!-- nested fields -->
<%= f.fields_for :listings do |builder| %>
<div class="field form-group">
<%= builder.label :type %> <br />
<%= builder.select :type, options_for_select(listing_types), {}, { :class => 'form-control', :required => 'true' } %>
</div>
<div class="field form-group">
<%= builder.label :title %> <br />
<%= builder.text_field :title, class: 'form-control', required: 'true' %> <br />
</div>
<div class="field form-group">
<%= builder.label :description %> <br />
<%= builder.text_area :description, class: 'form-control', required: 'true' %>
</div>
<div class="field form-group">
<%= builder.label :url %> <br />
<%= builder.text_field :url, class: 'form-control', required: 'true' %>
</div>
<div class="field form-group">
<%= builder.label :zipcode %> <br />
<%= builder.text_field :zipcode, class: 'form-control', size: '24x4', required: 'true' %>
</div>
<div class="field form-group">
<%= label_tag "upload images" %> <br />
<%= file_field_tag('listing_images_attributes_file', multiple: true, name: "user[listings_attributes][images_attributes][][image]", :required => 'true') %>
</div>
<div class="field form-group">
<%= builder.link_to_remove "Remove this listing", :onclick =>"myFunction(this)" %>
<hr style="height: 2px; background-color: lightgray;">
</div>
<% end %>
<p style="margin-bottom: 15px;"><%= f.link_to_add "Add a listing", :listings %></p>
<div class="actions">
<%= f.submit "Continue", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
Here are my models:
Listing Model:
class Listing
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :type, type: String
field :description, type: String
field :url, type: String
field :zipcode, type: String
# association between listing and user
embedded_in :user
# each listing has many images
embeds_many :images, :cascade_callbacks => true
accepts_nested_attributes_for :images, :allow_destroy => true
end
Image Model:
class Image
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paperclip
field :listing_id, type: Integer # what is the purpose of this field exactly?
embedded_in :listing
has_mongoid_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename"
end
Snippet from user controller update action (basically my create):
def update
#user = current_user
#have to include steps to validate zip code
case step
when :personal
#user.update_attributes(user_params)
when :listings
# should I build a listing as a default input?
# listing = #user.listings.build
#user.update_attributes(user_params)
end
render_wizard #user
end
def user_params
params.require(:user).permit(:first_name, :last_name, :zipcode, :bio, listings_attributes: [:title, :type, :description, :url, :zipcode,
{ images_attributes: [:image] }, :_destroy, :id])
end
Basically I am trying to get a user to be able to upload multiple listings, then multiple images under each listing. I wonder if this is possible using nested_attributes or will I have to handle the params manually? Also wondering whether my strong params are setup correctly. Here is a snippet from my server logs to show what my params look like:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HUpgUfukx3Rqb2Ye3su1/9ts4byC1BFrB3HOLXgQeJr8Vrcf9JyQYvR52FC5psLPZGD2x2DlzmlQRRgEXQWo2g==", "user"=>{"listings_attributes"=>{"0"=>{"type"=>"Airbnb", "title"=>"test", "description"=>"test", "url"=>"test", "zipcode"=>"test", "_destroy"=>"false", "id"=>"57a2b9ba9cd25d2d94110543"}}}, "listing"=>{"images_attributes"=>[{"image"=>#<ActionDispatch::Http::UploadedFile:0x000001016c2b20 #tempfile=#<Tempfile:/var/folders/vp/4hffkvd52gd317mgfqczqspm0000gn/T/RackMultipart20160804-11668-1fjlflq.jpg>, #original_filename="event1.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"listing[images_attributes][][image]\"; filename=\"event1.jpg\"\r\nContent-Type: image/jpeg\r\n">}]}, "commit"=>"Continue", "id"=>"listings"}
Sorry for the long description, but I was really hoping someone could answer this question. Thank you so much!
Related
I have a model for Organisation like
class Organisation
include ActiveModel::Model
attr_accessor :orguid,
:title, :firstname, :lastname, :role, :telephone, :extension, :email,
:name, :branch, :address1, :address2, :address3, :city, :state, :country, :zip
end
In my controller I have the following actions:
# frozen_string_literal: true
require 'cgi'
require 'json'
class OrganisationsController < ApplicationController
include Secured
before_action :set_api, only: %i[dashboard create]
before_action :user_info, only: %i[dashboard register]
def dashboard
#registration = #api.registered?
end
def register
#organisation = Organisation.new
end
def create
organisation_params
register_data = params[:organisation].to_h
register_data['oruid'] = org_uid
#api.register(register_data)
end
private
def set_api
#api = CoreApi.new(org_uid)
end
def user_info
#user_info = session[:userinfo].to_h
end
def org_uid
CGI.escape(user_info['uid'])
end
def organisation_params
params.require(:organisation).permit!
end
end
in my register.html.erb I have:
<h1> Register Your Organisation</h1>
<%= form_with model: #organisation, url: org_register_path do |f| %>
<div class="container">
<h2>Your Details</h2>
<div class="form-row">
<div class="form-group col-md-2">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control' %>
</div>
<div class="form-group col-md-5">
<%= f.label :first_name %>
<%= f.text_field :firstname, class: 'form-control' %>
</div>
<div class="form-group col-md-5">
<%= f.label :last_name %>
<%= f.text_field :lastname, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<%= f.label :role %>
<%= f.text_field :role, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<%= f.label :telephone %>
<%= f.telephone_field :telephone, class: 'form-control' %>
</div>
<div class="form-group col-md-2">
<%= f.label :extension %>
<%= f.text_field :extension, class: 'form-control' %>
</div>
<div class="form-group col-md-6">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control', readonly:'', value: #user_info['info']['name'] %>
</div>
</div>
</div>
<div class="container">
<h2>Organisation Details</h2>
<div class="form-row">
<div class="form-group col-md-6">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group col-md-6">
<%= f.label :branch %>
<%= f.text_field :branch, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<%= f.label :address_line_1 %>
<%= f.text_field :address1, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<%= f.label :address_line_2 %>
<%= f.text_field :address2, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<%= f.label :address_line_3 %>
<%= f.text_field :address3, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<%= f.label :city %>
<%= f.text_field :city, class: 'form-control' %>
</div>
<div class="form-group col-md-4">
<%= f.label :state %>
<%= f.text_field :state, class: 'form-control' %>
</div>
<div class="form-group col-md-4">
<%= f.label :country %>
<%= f.text_field :country, class: 'form-control' %>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<%= f.label :zip %>
<%= f.text_field :zip, class: 'form-control' %>
</div>
</div>
</div>
<div class="container">
<div class="form-row">
<div class="form-group col-md-12">
<%= f.button :Register, class: 'btn btn-primary' %>
</div>
</div>
</div>
<% end %>
and finally the register method in my core_api.rb is this:
def register(data)
body = data.to_json
puts ">> >> >> >> #{body.class} :: #{body}"
options = { headers: { 'Content-Type' => 'application/json' }, body: body }
response = self.class.post('/organisations', options)
#puts ">>>>>>>>>>>> #{response}"
end
and finally my routes.rb file contains:
Rails.application.routes.draw do
get '/' => 'home#show'
get '/auth/auth0/callback' => 'auth0#callback'
get '/auth/failure' => 'auth0#failure'
get '/logout', to: 'logout#logout', as: 'logout'
get '/organisations/dashboard', to: 'organisations#dashboard', as: 'org_dashboard'
get '/organisations/register', to: 'organisations#register', as: 'org_register'
post '/organisations/register', to: 'organisations#create'
root 'home#show'
end
now when I run the server and submit the form in the logs I get:
>> >> >> >> String :: {"title":"","firstname":"","lastname":"","role":"","telephone":"","extension":"","email":"alijy3#yahoo.com","name":"we","branch":"we","address1":"we","address2":"","address3":"","city":"we","state":"","country":"we","zip":"","oruid":"auth0%7C5e5388493d670c11be833bca","contact_id":0}
which to me looks like a proper json. But, since the api response was continually unsuccessful I intercepted the outgoing post with Postman to see what payload is being sent. To my surprise the payload is not flat json, but comes out like this:
I have 2 problems with this:
The api accepts items like address1, address2, city, etc. I believe I should send those rather than the currently showing organisation[address1], organisation[address2], etc.
The second problem is that I add the orguid after the form is submitted and before calling/posting to the api. But, although I can see it in the log messages, I don't see the orguid in the postman payload in any form.
I don't have any database on the server. Everything is fetch/posted/saved through the api. I've been reading about how to work with Activemodel and forms for a while and I haven't managed to get this resolved yet. Any help or explanation would be much appreciated.
No offense but this is a train wreck. You don't need to break every rails convention just because you're not using ActiveRecord in this specific case.
Start off by using ActiveModel::Attributes#attribute instead of Ruby's built in attr_accessor.
class Organisation
include ActiveModel::Model
include ActiveModel::Attributes
[:orguid, :title, :firstname, :lastname, :role, :telephone,
:extension, :email, :name, :branch,
:address1, :address2, :address3, :city, :state, :country, :zip]
.each do |name|
attribute name
end
# #todo write validations!
end
This creates attributes that act like ActiveRecord attributes and you can serialize the model properly with #organization.as_json.
Then lets just start fresh on that controller as there is just too much smell for it to be worth salvaging.
# routes.rb
resources :organisations, only: [:new, :create]
class OganizationsController < ApplicationController
# GET /organizations/new
def new
#organization = Organization.new
end
# POST /organizations
def create
# You never manually parse out incoming params - thats Rack's job.
# also since you have a model - USE IT!
#organization = Organization.new(organization_params) do |o|
o.orguid = org_uid
end
# validate the user input before you send it to an external API
if #organization.valid? && #api.register(#organization)
redirect_to '/somewhere'
else
render :new
end
end
private
# use monads here instead of callbacks!
def user_info
# Rails will serialize/deserialize hashes automatically
# from the session
session[:userinfo]
end
def org_uid
# Have no clue what the heck you're doing with CGI escape.
#org_uid ||= user_info['uid']
end
def api
#api ||= CoreApi.new(org_uid)
end
def organization_params
# You don't have any reason to use 'permit!' and give
# yourself a potential mass assignment vunerablity
params.require(:organization)
.permit(
:title, :firstname, :lastname, :role, :telephone,
:extension, :email, :name, :branch,
:address1, :address2, :address3, :city,
:state, :country, :zip
)
end
end
Rename the view /organizations/new.html.rb. At this point you should be able to stub out the API and do an integration test with valid and invalid input.
That whole session[:userinfo] thing still smells really bad - if you are taking the response from OAuth and shoving it into the session your setting yourself up for a really bad time as that can cause cookie overflows. Also in general in Rails if you're ever manually casting/serializing then its a really good sign that your doing something very wrong.
Have no clue really whats going on in your CoreApi class but if you are using HTTParty you should not do ANY manual JSON encoding.
# #fixme name is way to generic.
class CoreApi
include HTTParty
format :json # sets content type and encodes the content
# ...
def register(organization)
response = self.class.post('/organisations', #organization.as_json)
if response.success?
true
else
#organization.errors.add(:base, 'Could not be registered')
false
end
end
end
In my project I have API and its associated front end part. I am using rails version 5 . I am creating two models. One is user and another one is qualifications. Associations are given below.
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end
And, qualification model is
class Qualification
include Her::Model
belongs_to :user
attributes :school, :degree, :grade, :start_year, :end_year
end
My strong params in users controller is
def user_params
user: params.require(:user).permit(:id, :name, :email, :phone, :contact_email, :about,:image_url,
{specialization_ids: []},
qualifications_attributes: [:id, :school, :degree, :grade, :start_year, :end_year, :_destroy]
end
My user form, where i have provided nested_form is
<div class="row">
<div class="container">
<div class="signup-bg col-md-12">
<h3 style="color:white;"> General Info </h3>
<hr>
<%= simple_nested_form_for #user do |f| %>
<%= f.input :email, input_html: {class: "input-user-form"} %>
<%= f.input :name, input_html: {class: "input-user-form"} %>
<%= f.input :phone , input_html: {class: "input-user-form"}%>
<%= f.input :contact_email, input_html: {class: "input-user-form"} %>
<%= f.input :about, as: :text, input_html: {class: "input-user-form"}%>
<label class="control-label">Selected Specializations</label>
<ul class="list-group">
<% #user.specializations.each do |sp| %>
<li class="list-group-item tags">
<%= check_box_tag "user[specialization_ids][]", sp["id"], true%>
<%= sp["name"] %>
</li>
<% end %>
</ul>
<h4 style="color:white;"><center>Select Your Specializations</center></h4>
<div class="row">
<div class="col-md-12">
<div class="input-group">
<input id="searchText" type="text" class="input-signup" placeholder="Search Specialization" style="z-index:0;">
<span class="input-group-btn">
<button class="btn btn-default btn-go" type="button">Go!</button>
</span>
</div>
<div class="col-md-12 col-sm-12 col-lg-12" id="specializationHolder" style="min-height:85px;"></div>
</div>
</div>
<h3 style="color:white;"> Qualification Info </h3>
<hr>
<div id="qualification_holder">
<%= f.simple_fields_for :qualifications, #qualification do |g| %>
<%= g.input :school,input_html: {class: "input-user-form"} %>
<%= g.input :degree, input_html: {class: "input-user-form"} %>
<%= g.input :grade, as: :integer, input_html: {class: "input-user-form"} %>
<%= g.input :start_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<%= g.input :end_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<% end %>
</div>
</br>
<div class="col-md-4">
<%= f.link_to_add :qualifications, data: {target: "#qualification_holder"}, class: "btn-signup-submit" do %>
<b> + </b> Add Qualifications
<% end %>
</div>
<div id="wrapper">
<label class="control-label file optional" for="user_image_url">Profile Picture</label>
<input class="file optional" type="file" name="user[image_url]" id="fileUpload">
</div>
<div class="form-group text-center">
<button type="submit" class="btn-signup-submit">Save Changes</button>
</div>
<% end %>
</div>
</div>
</div>
But, I am getting an error undefined error "reflect_on_association", pointing towards "link_to_add". I am Stuck here for a week. Is there any solutions for it.. If so, please help me.
Thanks in advance.
I had the same error. The solution is to add the class_name option on your problematic association, with no nesting on the class name.
In your case, you can try:
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications, class_name: "::Qualification"
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end
I have a triple nested attributes that I am trying to capture all the information for in one form. In short I have an event that has many event_locations which have many event_dates which have many event_roles. I have the form in my event new. I think the issues is in my events model and I need to say that there is going other "accepts_nested_attributes_for" for event_dates and event_roles because right now in the event_params I do get the event and event_location information.
events controller
def create
#event = Event.new(event_params)
if #event.save
redirect_to #event, notice: 'Event was successfully created.'
else
render :new
end
end
private
def event_params
params.require(:event).permit(:name, :event_details, event_locations_attributes: [:label, :address, :zip, :state, :country, :notes], event_dates_attributes: [:event_date, :start_time, :end_time], event_roles_attributes: [:type, :hourly_rate, :quantity])
end
event.rb
class Event < ActiveRecord::Base
has_many :event_locations, :dependent => :destroy
has_many :event_dates, through: :event_locations
belongs_to :agency
accepts_nested_attributes_for :event_locations, :allow_destroy => true
end
event new
<h3> New Event </h3>
<%= form_for #event do |f| %>
<%= f.text_field :name, :required => true, placeholder: "Name", :maxlength => 200, class: "form-control title_input_field" %><br>
<%= f.text_area :event_details, :required => true, placeholder: "What are some of the event details", size: "100x10", class: "input_field2" %> <br>
<h3> Event Locations </h3>
<%= f.fields_for :event_locations do |builder| %>
<%= render "location_fields", :f => builder %>
<% end %>
<%= f.submit "Create!", :class => 'submit_button' %>
<% end %>
_location_fields.html.erb
<p>
<div class="row">
<div class="col-sm-5">
<%= f.text_field :label, :required => true, placeholder: "Label", :maxlength => 1000, class: "form-control title_input_field" %><br>
</div>
<div class="col-sm-5">
<%= f.text_area :notes, :required => true, placeholder: "Notes", :maxlength => 200, class: "form-control title_input_field" %><br>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<%= f.text_field :address, :required => true, placeholder: "Address", :maxlength => 200, class: "form-control title_input_field" %><br>
</div>
<div class="col-sm-2">
<%= f.text_field :state, :required => true, placeholder: "State / Provience", :maxlength => 200, class: "form-control title_input_field" %><br>
</div>
<div class="col-sm-2">
<%= f.text_field :country, :required => true, placeholder: "Country", :maxlength => 200, class: "form-control title_input_field" %><br>
</div>
<div class="col-sm-2">
<%= f.text_field :zip, :required => true, placeholder: "Zip Code", :maxlength => 200, class: "form-control title_input_field" %><br>
</div>
</div>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Location" %>
</p>
<%= f.fields_for :event_date do |builder| %>
<%= render "event_date_fields", :f => builder %>
<% end %>
--
_event_date_fields.html.erb
<p>
<h3> Event Date </h3>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<div class='input-group date' id='datetimepicker1' />
<%= f.text_field :event_date, placeholder: "Select Event Date", class: "form-control", type:'text' %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div> <!-- date -->
</div> <!-- form-group -->
</div>
<div id="datepairExample">
<div class="col-sm-4">
<%= f.text_field :start_time, placeholder: "Start Time", class: 'time start form-control' %>
</div>
<div class="col-sm-1">to</div>
<div class="col-sm-4">
<%= f.text_field :end_time, placeholder: "End Time", class: 'time end form-control' %>
</div>
</div>
</div>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Date" %>
</p>
<%= f.fields_for :event_role do |builder| %>
<%= render "event_role_fields", :f => builder %>
<% end %>
In short, you need to allow all the models to allow nested attributes for their direct associations, so:
class Event < ActiveRecord::Base
has_many :event_locations, :dependent => :destroy
accepts_nested_attributes_for :event_locations, :allow_destroy => true
class EventLocation < ActiveRecord::Base
has_one :event_date
accepts_nested_attributes_for :event_date, allow_destroy: true
class EventDate < ActiveRecord::Base
has_one :event_role
accepts_nested_attributes_for :event_role, allow_destroy: true
Then your strong params should look like this:
params.require(:event).permit(:name, :event_details,
event_locations_attributes: [:label, :address, :zip, :state, :country, :notes,
event_date_attributes: [:event_date, :start_time, :end_time,
event_role_attributes: [:type, :hourly_rate, :quantity]]])
I have a model named Page and one named Medium. Medium is for uploaded images. The two models are connected with a rich HABTM association.
I want to be able to upload images through my page edit form. However i got a strange result. I am only getting the file field when the page already have an image associated with it. What can cause this?
This is my page model:
class Page < ActiveRecord::Base
attr_accessible :content, :title, :url, :status, :excerpt, :media_attributes
belongs_to :users
has_many :page_medium
has_many :media, :through => :page_medium
accepts_nested_attributes_for :media
acts_as_url :title, :only_when_blank => true, :sync_url => true
acts_as_list
STATUS = %w[draft published]
def to_param
"#{url}" # or whatever you set :url_attribute to
end
validates :title, :status, :url, :presence => true
end
This is my form:
<%= simple_form_for #page, :html => { :class => 'page-form' } do |f| %>
<div class="span9">
<div class="row">
<%= f.input :title, input_html: { class: 'span6'}, wrapper_html: { class: 'span6'} %>
<%= f.input :url, input_html: { class: 'span3'}, wrapper_html: { class: 'span3'} %>
</div>
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active">Content</li>
<li>Excerpt</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<%= f.input :content, label: false, input_html: { class: 'wysihtml5 span9' } %>
</div>
<div class="tab-pane" id="tab2">
<%= f.input :excerpt, label: false, input_html: { class: 'wysihtml5 span9 excerpt' }%>
</div>
</div>
</div>
</div>
<div class="span3">
<div class="well">
<fieldset>
<legend>Options</legend>
</fieldset>
<%= f.input :status , collection: Page::STATUS.map { |s| [s.humanize, s] }, prompt: '- Select page status -' %>
<div class="form-actions">
<%= f.button :submit, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
pages_path, :class => 'btn' %>
</div>
</div>
</div>
<div class="span9">
<%= f.simple_fields_for :media do |m| %>
<%= m.input :image, as: :file %>
<% end %>
</div>
<% end %>
<%= render :partial => 'display_images' %>
Please comment if you need any more information or code. All tips, answers or constructive comments are appreciated. :)
this code:
<%= f.simple_fields_for :media do |m| %>
<%= m.input :image, as: :file %>
<% end %>
will only allow you to update the images you already have.
To add more images I suggest you follow this episode of railscast: http://railscasts.com/episodes/196-nested-model-form-revised
I'm doing a nested form in Rails 3.2.5, but when I add the accepts_nested_attributes_for my fields_for disappear (they just stop showing in my form).
This are my models:
class Product < ActiveRecord::Base
attr_accessible :title, :description, :variants_attributes
has_many :variants
accepts_nested_attributes_for :variants
validates :title, presence: true
end
My second model is
class Variant < ActiveRecord::Base
belongs_to :product
attr_accessible :price, :sku, :weight, :inventory_quantity, :product_id
end
And in my view I have
<%= form_for [:admin, #product], :html => {:multipart => true} do |f| %>
<%= render 'admin/shared/error_messages', object: f.object %>
<fieldset>
<legend>Producto Nuevo</legend>
<div class="control-group">
<%= f.label :title, 'Título', class: 'control-label' %>
<div class="controls">
<%= f.text_field :title %>
</div>
</div>
**<%= f.fields_for :variants do |variant| %>
<%= render 'inventory_fields', f: variant %>
<% end %>**
<div class="actions">
<%= f.submit 'Guardar', class: 'btn btn-primary' %>
</div>
<% end %>
_inventory_fields.html.erb
<div class="control-group">
<%= f.label :inventory_quantity, 'How many are in stock?', class: 'control-label' %>
<div class="controls">
<%= f.text_field :inventory_quantity, class: 'span1', value: '1' %>
</div>
</div>
The part between the ** is the one that is not being printed. And when I remove accepts_nested_attributes_for in my Product model fields_for start showing again but my form won't work.
What's happening?!?!
In the controller new action call
#product.varients.build
That should create 1 in memory varient in the product varient collection and it should bind to the fields for