Sl.aze
Sl.aze

Reputation: 53

Multiple models in new form - Rails

I have a two models. Product and ProductPrice (with tables products and product_prices), each product has one price. I want to create a form for both models but after replicating the solutions to similar scenarios my form still doesn't show the field for price.

class Product < ActiveRecord::Base
    belongs_to :user
    has_one :ProductPrice

    accepts_nested_attributes_for :ProductPrice
end

class ProductPrice < ActiveRecord::Base
    belongs_to :Product
end

class ProductsController < ApplicationController

    def new
        @product = Product.new
        @product_price = @product.build_ProductPrice
    end

end


<%= form_for @product, url: user_product_path do |f| %>
   <div class="form-group">
        <%= f.text_field :product_name, placeholder: 'name', class: 'form-  control' %>
    </div>

   <% f.fields_for @product_price do |b| %>
   <%= b.text_field :price, placeholder: 'Enter price', class: 'form-control' %>
   <%end%>
<% end%>

Any ideas? did I reference the models correctly?

Edit: Fixed. It needed to be <%= fields_for .... The equal sign was missing

Upvotes: 1

Views: 43

Answers (2)

Lymuel
Lymuel

Reputation: 574

Try this

class Product < ActiveRecord::Base
    belongs_to :user
    has_one :product_price

    accepts_nested_attributes_for :product_price
end

class ProductPrice < ActiveRecord::Base
    belongs_to :product
end

class ProductsController < ApplicationController
    def new
        @product = Product.new
        @product.product_price.build
    end
end


<%= form_for @product, url: user_product_path do |f| %>
    <div class="form-group">
        <%= f.text_field :product_name, placeholder: 'name', class: 'form-control' %>
    </div>

    <%= f.fields_for :product_price do |b| %>
        <%= b.text_field :price, placeholder: 'Enter price', class: 'form-control' %>
    <%end%>
<% end%>

Upvotes: 1

Cimm
Cimm

Reputation: 4775

First thing that stands out is the use of uppercases in Rails. Yes, you are right to write class ProductPrice but you should use snake cases everywhere else, like: product_price.

Can you try the following:

class Product < ActiveRecord::Base
  belongs_to :user
  has_one :product_price

  accepts_nested_attributes_for :product_price
end

class ProductPrice < ActiveRecord::Base
  belongs_to :product
end

class ProductsController < ApplicationController
  def new
    @product = Product.new
    @product_price = @product.product_price.build
  end
end


<%= form_for @product, url: user_product_path do |f| %>
  <div class="form-group">
    <%= f.text_field :product_name, placeholder: 'name', class: 'form-control' %>
  </div>

  <% f.fields_for @product_price do |b| %>
    <%= b.text_field :price, placeholder: 'Enter price', class: 'form-control' %>
  <%end%>
<% end%>

Side note but product.product_price.price feels weird. Depends on the rest of your structure but it feels unnecessary to build an association here, simply store the price on the product.

Upvotes: 0

Related Questions