Created
February 1, 2014 21:51
-
-
Save anonymous/8759464 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= nested_form_for @product, html: {class: "form", role: "form"} do |f| %> | |
<div class="from-group"> | |
<%= f.label :name, "New Product", class: "" -%> | |
<%= f.text_field :name, | |
class: "form-control", | |
placeholder: "Product Name" -%> | |
</div> | |
<%= f.label :details -%> | |
<%= f.fields_for :details do |d| -%> | |
<div class="form-group"> | |
<div class="row"> | |
<div class="col-sm-2"> | |
<%= d.label :name %> | |
<%= d.text_field :name, class: "form-control", width: "" -%> | |
</div> | |
<div class="col-sm-2"> | |
<%= d.label :required_at, "Required" -%> | |
<%= d.select(:required_at, [ | |
["", ""], | |
['App', 0], | |
['Red', 1], | |
['Amber', 2], | |
['Green', 3] | |
], | |
{}, | |
{class: "form-control"} | |
)-%> | |
</div> | |
<div class="col-sm-1"> | |
<label> </label> | |
<%= d.link_to_remove class: "form-control btn btn-sm btn-danger", | |
title: "Remove Ingredient" do -%> | |
<span class="glyphicon glyphicon-remove"></span> | |
<% end %> | |
</div> | |
</div> | |
</div> | |
<%= d.label :properties %> | |
<%= d.fields_for :properties, @detail do |p| -%> | |
<div class="form-group"> | |
<div class="row"> | |
<div class="col-sm-3"> | |
<%= p.label :name %> | |
<%= p.text_field :name, class: "form-control", width: "" -%> | |
</div> | |
<div class="col-sm-1"> | |
<label> </label> | |
<%= p.link_to_remove class: "form-control btn btn-sm btn-danger", | |
title: "Remove Ingredient" do %> | |
<span class="glyphicon glyphicon-remove"></span> | |
<% end %> | |
</div> | |
</div> | |
</div> | |
<% end -%> | |
<div class="form-group"> | |
<%= d.link_to_add :properties, | |
class: "btn btn-sm btn-info" do %> | |
<i class="glyphicon glyphicon-plus"></i> | |
Add Property | |
<% end %> | |
</div> | |
<% end -%> | |
<div class="form-group"> | |
<%= f.link_to_add :details, | |
class: "btn btn-sm btn-info" do %> | |
<i class="glyphicon glyphicon-plus"></i> | |
Add Detail | |
<% end %> | |
</div> | |
<%= f.submit "Create Product", | |
class: "btn btn-success" %> | |
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Detail < ActiveRecord::Base | |
belongs_to :product | |
has_many :properties, :dependent => :destroy | |
validates :name, :presence => true | |
accepts_nested_attributes_for :properties, reject_if: :all_blank, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
has_many :details, :dependent => :destroy | |
validates :name, :presence => true | |
accepts_nested_attributes_for :details, reject_if: :all_blank, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductsController < ApplicationController | |
# Make an all and new object for every action | |
before_filter :load | |
# This page has no main page so far | |
def index | |
@products = Product.all | |
end | |
def new | |
@product = Product.new | |
3.times {@product.details.build} | |
@detail = Detail.new | |
3.times {@detail.properties.build} | |
end | |
# Create a new product | |
def create | |
@product = Product.new(safe_params) | |
if @product.save | |
respond_to do |format| | |
format.html { redirect_to root_path, notice: "Created #{@product.name}."} | |
format.js {} | |
end | |
else | |
redirect_to new_product_path | |
end | |
end | |
# Edit a product.. | |
def edit | |
@product = Product.find(params[:id]) | |
end | |
# Update a product | |
def update | |
@product = Product.find(params[:id]) | |
if @product.update_attributes(safe_params) | |
flash[:notice] = "#{@product.name} updated." | |
@products = Product.all | |
end | |
end | |
# Delete product | |
def destroy | |
@product = Product.find(params[:id]) | |
@product.destroy | |
flash[:notice] = "#{@product.name} deleted." | |
@products = Product.all | |
end | |
# Show products and details | |
def show | |
@product = Product.find(params[:id]) | |
@details = @product.details | |
@properties = @details.properties | |
# @details = Detail.find_all_by_product_id(params[:id]) | |
# @detail = Detail.find_by_product_id(params[:id]) | |
end | |
private | |
# Load all products | |
def load | |
@products = Product.all | |
@product = Product.new | |
end | |
# Permit safe params | |
def safe_params | |
params.require(:product).permit(:product, | |
:name, | |
details_attributes: [:name, :required_at], | |
properties_attributes: [:name] | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Property < ActiveRecord::Base | |
belongs_to :detail | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment