2014-11-06 18 views
10

Nel tentativo di creare due campi nascosti, e uno si presenta nessun problema, ma l'altro che viene dal formato nidificato non lo fasimple_fields_for non presentandosi [rotaie 4]

product.rb

class Product < ActiveRecord::Base 
    has_many :product_options, dependent: :destroy 
    accepts_nested_attributes_for :product_options, allow_destroy: true, :reject_if => proc { |x| x[:option_name].blank? } 
    belongs_to :user 
end 

product_option.rb

class ProductOption < ActiveRecord::Base 
    belongs_to :product 
end 

products_controller.rb

class ProductsController < ActionController::Base 
    layout "application" 

    def index 
    @products = Product.all 
    @current_user = Client.find_by(id: session[:client]) 
    if @current_user.redeemed == true 
     redirect_to root_path 
    end 
    end 

    def show 
    @product = Product.find(params[:id]) 
    @product_option = @product.product_options.find(params[:id]) 
    @current_user = Client.find_by(id: session[:client]) 
    @current_user.update(:product_option => @product_option.option_name) 
    @current_user.update(:selected_product => @product.id) 
    render :nothing => true 
    end 

    private 
    def product_params 
     params.require(:product).permit(:name, :id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, 
      :product_options_attributes => [:id, :option_name, :ranking, :total_redeemed, :product_id]) 
    end 
end 
.210

_form.html.erb

<%= simple_form_for Product.new, :method => "post",:remote => true, :class => "item_prompt" do |f| %> 
     <%= f.hidden_field :id, :class => 'product_id' %> 
     <%= f.simple_fields_for :product_options do |ff| %> 
      <%= ff.hidden_field :id, :class => 'product_option_id' %> 
     <% end %> 
     <%= f.submit "Yep!", :class => "yep ready_button confirm_button", :name => "confirm_button" %> 
    <% end %> 

output HTML

<form accept-charset="UTF-8" action="/products" class="simple_form new_product" data-remote="true" id="new_product" method="post" novalidate="novalidate"> 
    <input class="product_id" id="product_id" name="id" type="hidden" value="240"> 
    <input class="yep ready_button confirm_button" name="confirm_button" type="submit" value="Yep!"> 
<form> 

risposta

17

ho capito questo, ... il problema era

ciclo volontà fields_for nel corso di un associazione di raccolta, il rendering come molte volte in quanto ci sono elementi in esso, che significa 0 volte se l'associazione è vuota

quindi per risolvere il problema ho dovuto aggiungere

@product = Product.new 
@product.product_options.build 

all'azione di indice nel controller.

+0

Grazie per aver lasciato una risposta per i posteri. Mi ha aiutato! – fastredshoes

Problemi correlati