Hello i have a Category view. You can make a category such as "Shirt" then select from already made sizes like XS, S, M, Large. When you create a new category sizes comes up nill.
It should come up with all the categories selected.
See below size_id: nil
2.2.1 :006 > Category.last
Category Load (0.4ms) SELECT "categories".* FROM "categories" ORDER BY "categories"."id" DESC LIMIT 1
=> #<Category id: 30, name: "test", ancestry: "20", created_at: "2015-09-14 11:09:07", updated_at: "2015-09-14 11:09:07", size_id: nil>
When I use the following code it passes the id <%= f.collection_select :size_id, Dize.order(:title), :id, :title, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
so i know the problem has to do with my form.
My view. Im using simple form gem. So im currently using <%= f.association :sizes %>
<div class="container">
<div class=“row”>
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_form_for(@category) do |f| %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.association :sizes %>
<%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
</div>
<div class="form-actions"><%= f.button :submit %></div>
<% end %>
</div>
</div>
</div>
</div>
</div>
Category controller.
class CategoriesController < ApplicationController
before_action :set_category, only: [:show]
before_action :admin_user, only: [:destroy, :index, :edit, :show]
def index
@categories = Category.all
end
def show
@tags = Item.where(category_id: @category.id).tag_counts_on(:tags)
if params[:tag]
@items = Item.tagged_with(params[:tag])
else
@items = Item.where(category_id: @category.id).order("created_at DESC")
end
end
def new
@category = Category.new
end
def edit
@category = Category.find(params[:id])
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to @category
flash[:success] = "You have created a new category"
else
flash[:danger] = "Your category didn't save"
render "new"
end
end
def update
@Cateogry = Category.find(params[:id])
if @Cateogry.update(category_params)
redirect_to @Cateogry
flash[:success] = 'Category was successfully updated.'
else
render "edit"
end
end
def destroy
Category.find(params[:id]).destroy
flash[:success] = "Category deleted"
redirect_to categories_path
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :parent_id, :size_id)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.try(:admin?)
end
end
My scheme
ActiveRecord::Schema.define(version: 20150914095836) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "ancestry"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "size_id"
end
add_index "categories", ["ancestry"], name: "index_categories_on_ancestry"
create_table "items", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.integer "category_id"
end
add_index "items", ["user_id", "created_at"], name: "index_items_on_user_id_and_created_at"
add_index "items", ["user_id"], name: "index_items_on_user_id"
create_table "sizes", force: :cascade do |t|
t.text "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
create_table "users", force: :cascade do |t|
t.string "username"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.string ">"
t.datetime "reset_sent_at"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.text "description"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
Aucun commentaire:
Enregistrer un commentaire