【问题标题】:Create categories in rails App在 rails App 中创建类别
【发布时间】:2017-01-28 12:17:49
【问题描述】:

我想在我的应用中创建类别: 我受到this question的启发

但是当@RailsGuy 说:“通过类别控制器和表单创建一些类别(我不认为,我需要告诉你这些东西,你可以自己做)” 我感到迷茫...

如何以及在哪里创建我的类别列表?狗、猫、鸟…… 我看到它可以在控制台中完成,但我会为每个类别显示不同的图片...

这是我的 _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"

我的模型:

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

非常感谢您的帮助

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    将图片存储在资产中。并在表单/视图中使用条件?:

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

    类似的东西?

    史蒂夫。

    【讨论】:

      【解决方案2】:

      另一篇文章说使用控制器和表单来创建新类别。如果您使用脚手架创建模型类别,您将在控制器中拥有newcreate 操作以及new.html.erbindex.html.erb(或者.slim,如果您正在使用它)。

      您可以在其中创建类别,然后在 Tuto 模型中选择一个。

      有意义吗?

      史蒂夫。

      【讨论】:

      • 我需要默认类别 :) 例如“狗、猫、鸟”
      • 如果您对Category 的实例进行硬编码并确保newcreate 操作中没有任何内容,或者将这些操作限制为仅限管理员,那么只有那些操作您创建对您的用户可用。您可以在生产环境中通过控制台创建它们,或者作为管理员用户。
      • 我知道控制台...但我不确定它是否适用于相应的图片...如果选择此项目显示此图像...你看到了吗?它必须在代码中......但我不知道如何:'(
      • 嘿,你在我的脑海中读到,这就是我正在尝试的......也许我最终会使用控制台;)
      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      相关资源
      最近更新 更多