Search code examples
ruby-on-railsnomethoderrorrailscastsdynamic-forms

dynamic forms in rails, NoMethodError


I'm new to rails, and I was building something based off this railscast,

https://github.com/railscasts/403-dynamic-forms

Since this is very outdated, I'm unable to download one of the old gems that it is reliant on - at least, using RVM - so I can't really check it.

However, someone updated it from rails 3 to 4.2, https://github.com/asang/403-dynamic-forms, but I keep getting an undefined method 'fields' for nil:NilClass error.

Product.rb

class Product < ActiveRecord::Base
  belongs_to :product_type
  serialize :properties, Hash

 validate :validate_properties

 def validate_properties
   product_type.fields.each do |field|
    if field.required? && properties[field.name].blank?
     errors.add field.name, "must not be blank"
    end
  end
 end
end

On a side question, when changing from rails 3 to rails 4.2, how does the product_field.rb access the :field_type, and :name if it doesn't have a controller?

Product_field.rb - Rails 3

class ProductField < ActiveRecord::Base
 belongs_to :product_type
 attr_accessible :field_type, :name, :required
end

Product_field.rb - Rails 4.2

class ProductField < ActiveRecord::Base
 belongs_to :product_type
end

Product_types_controller.rb

class ProductTypesController < ApplicationController

 def index
  @product_types = ProductType.all

   respond_to do |format|
     format.html 
     format.json { render json: @product_types }
   end
 end


 def show
   @product_type = ProductType.find(params[:id])

   respond_to do |format|
     format.html 
     format.json { render json: @product_type }
   end
 end


 def new
   @product_type = ProductType.new

   respond_to do |format|
    format.html 
    format.json { render json: @product_type }
   end
  end

 def edit
  @product_type = ProductType.find(params[:id])
 end


 def create
  @product_type = ProductType.new(product_type_params)

  respond_to do |format|
    if @product_type.save
      format.html { redirect_to @product_type, notice: 'Product type was    successfully created.' }
    format.json { render json: @product_type, status: :created, location: @product_type }
    else
    format.html { render action: "new" }
    format.json { render json: @product_type.errors, status: :unprocessable_entity }
    end
   end
  end


def update
  @product_type = ProductType.find(params[:id])

  respond_to do |format|
    if @product_type.update_attributes(product_type_params)
      format.html { redirect_to @product_type, notice: 'Product type was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @product_type.errors, status: :unprocessable_entity }
    end
   end
 end


def destroy
  @product_type = ProductType.find(params[:id])
  @product_type.destroy

  respond_to do |format|
    format.html { redirect_to product_types_url }
    format.json { head :no_content }
  end
 end

 def product_type_params
   params.require(:product_type).permit(
        :name, fields_attributes: [ :field_type, :name, :required ] )
 end
end

Product_controller.rb

class ProductsController < ApplicationController
      def index
        @products = Product.all
      end

      def show
        @product = Product.find(params[:id])
      end

      def new
        @product = Product.new(product_type_id: params[:product_type_id])
      end

      def edit
        @product = Product.find(params[:id])
      end

      def create
        @product = Product.new(product_params)
        if @product.save
          redirect_to @product, notice: 'Product was successfully created.'
        else
          render action: "new"
        end
      end

      def update
        @product = Product.find(params[:id])
        if @product.update_attributes(product_params)
          redirect_to @product, notice: 'Product was successfully updated.'
        else
          render action: "edit"
        end
      end

      def destroy
        @product = Product.find(params[:id])
        @product.destroy
        redirect_to products_url
      end

      private

      def product_params
        params.require(:product).permit(:name, :price, :product_type_id,
                                    :properties)
      end
    end

_form.html.erb

<%= form_for @product do |f| %>
      <% if @product.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>
          <ul>
          <% @product.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <%= f.hidden_field :product_type_id %>

      <div class="field">
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </div>
      <div class="field">
        <%= f.label :price %><br />
        <%= f.text_field :price %>
      </div>

      <%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
        <% @product.product_type.fields.each do |field| %>
          <%= render "products/fields/#{field.field_type}", field: field, f: builder %>
        <% end %>
      <% end %>

      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

Log

Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms)

    ActionView::Template::Error (undefined method `fields' for nil:NilClass):
        22:   </div>
        23:
        24:   <%= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder| %>
        25:     <% @product.product_type.fields.each do |field| %>
        26:       <%= render "products/fields/#{field.field_type}", field: field, f: builder %>
        27:     <% end %>
        28:   <% end %>
      app/views/products/_form.html.erb:25:in `block (2 levels) in _app_views_products__form_html_erb__38604707748806799_70122829936940'
      app/views/products/_form.html.erb:24:in `block in _app_views_products__form_html_erb__38604707748806799_70122829936940'
      app/views/products/_form.html.erb:1:in `_app_views_products__form_html_erb__38604707748806799_70122829936940'
      app/views/products/new.html.erb:3:in `_app_views_products_new_html_erb__487839569246893265_70122829438980'

Someone on railscast posted a comment having this same issue, other replied saying that it had to do with leaving out the <%= f.hidden_field :product_type_id %> am I missing something? Thanks for taking the time to look over this, most likely, silly mistake.


Solution

  • The issue is with how you're instantiating your @product on this line in ProductsController#new:

    def new
      @product = Product.new(product_type_id: params[:product_type_id])
    end
    

    You're referencing the product_type by its id, rather than as a concrete reference. Since @product isn't saved in your new action @product.product_type is never loaded from the database and will always be nil. To fix, load the product_type and reference it directly from the new product:

    def new
      product_type = ProductType.find(params[:product_type_id])
      @product = Product.new(product_type: product_type)
    end