Skip to content

Instantly share code, notes, and snippets.

@indrekj
Created January 28, 2013 23:17
Show Gist options
  • Save indrekj/4660197 to your computer and use it in GitHub Desktop.
Save indrekj/4660197 to your computer and use it in GitHub Desktop.
module Admin
class CategoriesController < BaseController
def new
@form = CategoryForm.new
end
def create
@form = CategoryForm.new(params[:category])
if @form.valid?
category = CreatesCategory.new(
@form.attributes.merge(company_id: current_company.id)
).call
show_notice
redirect_to admin_category_products_path(category.id)
else
render :new
end
end
end
end
class CategoryForm
include FunkyForm
IMAGES = %w(
not_important
)
model "Category"
attribute :id, Integer
attribute :name, String
attribute :image, String, default: IMAGES.first
validates :name, presence: true, length: {within: 3..20}
validates :image, presence: true, inclusion: {in: IMAGES}
end
= simple_form_for [:admin, @form], html: {class: "form-horizontal"} do |f|
= f.input :name, placeholder: "Category name"
= f.input :image, as: :hidden
.control-group.string.required
= f.label :image
.controls.category-images
- CategoryForm::IMAGES.each do |image|
/ removed
.form-actions
= f.button :submit, class: "btn-primary"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment