简体   繁体   中英

Create categories in rails App

I would like to create categories in my app: I got inspired by this question

But when @RailsGuy says: "Create some categories by categories controller and form (I don't think, I need to tell you that stuff, you are able to do it yourself)" I felt lost...

How and where do I create my list of categories? dogs, cats, birds... I saw that it could be done in the console but I will display a different pictures for each categories...

Here is my _form.html.slim

= simple_form_for @tuto do |f|
  - if @tuto.errors.any?
    #error_explanation
      h2 = "#{pluralize(@tuto.errors.count, "error")} prohibited this tuto from being saved:"
      ul
        - @tuto.errors.full_messages.each do |message|
          li = message
  = f.hidden_field :user_id, value: current_user.id
  = f.input  :title
  = f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose Category"}
  = f.input :content, as: :text, input_html: { rows: "15" }
  = f.button :submit, "Save"

my models:

tuto_model.rb

class Tuto < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
  belongs_to :category
  validates :category, presence: true
end

categoty_model.rb

class Category < ActiveRecord::Base
  has_many :tutos
end

schema.rb

ActiveRecord::Schema.define(version: 20160920133801) do

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.string   "image"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  create_table "tutos", force: :cascade do |t|
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.string   "title"
    t.text     "content"
    t.integer  "user_id"
    t.integer  "category_id"
  end

  add_index "tutos", ["user_id"], name: "index_tutos_on_user_id"

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "first_name"
    t.string   "last_name"
    t.boolean  "admin"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

  create_table "votes", force: :cascade do |t|
    t.integer  "votable_id"
    t.string   "votable_type"
    t.integer  "voter_id"
    t.string   "voter_type"
    t.boolean  "vote_flag"
    t.string   "vote_scope"
    t.integer  "vote_weight"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
  add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"

end

thanks a lot for your help

The other post says to use the controller and form to create new categories. If you used scaffold to create the model Category, you'll have the new & create action in the controller and a new.html.erb and index.html.erb (or.slim if you're using that).

You can create your categories in there, then select one in your Tuto model.

Make sense?

Steve.

Have the pictues stored in assets. And in the form/view use a conditional?:

<% if tuto.category == dog %> <%= image_tag 'dog.jpg' %> <% end %>

Something like that?

Steve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM