Skip to content

Instantly share code, notes, and snippets.

@ExByt3s
Last active August 29, 2015 14:15
Show Gist options
  • Save ExByt3s/63f7fded43d14ceb103e to your computer and use it in GitHub Desktop.
Save ExByt3s/63f7fded43d14ceb103e to your computer and use it in GitHub Desktop.
auditions_controller.rb
class Audition < ActiveRecord::Base
attr_writer :current_step
attr_accessor :brand
before_create :audition_time
belongs_to :category
has_many :prices
has_many :specifications
accepts_nested_attributes_for :specifications, reject_if: :all_blank, allow_destroy: true
def current_step
@current_step || steps.first
end
def steps
%w[name_categories descriptions]
end
def next_step
self.current_step = steps[steps.index(current_step)+1]
end
def previous_step
self.current_step = steps[steps.index(current_step)-1]
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
def all_valid?
steps.all? do |step|
self.current_step = step
valid?
end
end
# return wholesales price of single product when specified number of products
# n_products - number of products
def price_of(n_products)
p_price=0.0
prices.each do |p|
p_price=p.price if(n_products>=p.quantity_from)
end
return p_price
end
private
def audition_time
empieza = DateTime.now.strftime("%d/%m/%Y")
timeout = DateTime.now. + 30.day
termina = timeout.strftime("%d/%m/%Y")
self.timestart = empieza
self.timeend = termina
end
def activare
self.active =1
end
end
class AuditionsController < ApplicationController
def index
@auditions = Audition.all
end
def new
@auditions = Audition.new
@auditions.specifications.build
end
def create
@auditions = Audition.new(audition_params)
if @auditions.save
respond_to do |format|
format.html { redirect_to root_path, notice: "New created: #{@auditions.titulo}"}
end
else
redirect_to new_audition_url
end
end
def subcategories_by_category
@sub = Category.subs(params[:sub]).map { |sub| [sub.name, sub.id] }
@child = Category.subs(params[:subs]).map { |sub| [sub.name, sub.id] }
#.map { |sub| [ sub.name , sub.id] }
respond_to do |format|
format.js
end
end
private
def audition_params
params.require(:audition).permit(:titulo, :descripcion,
specifications_attributes: [:brand]
)
end
def specsificacions
#for test
params.require(:specifications).permit(:brand)
end
end
<div class="frm_imp">
<%= form_for (@auditions) do |f| %>
<%= f.text_field :titulo %>
<%= f.text_field :descripcion %>
<%= fields_for :specifications do |specifications| %>
<%= specifications.text_field :brand %>
<% end %>
<%= f.submit %>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment