我想在我的应用程序中创建分类:我受到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"my models:
tuto_model.rb
class Tuto < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :category
validates :category, presence: true
endcategoty_model.rb
class Category < ActiveRecord::Base
has_many :tutos
endschema.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非常感谢你的帮助
发布于 2016-09-20 14:56:18
将图片存储在资产中。在表单/视图中使用条件?:
<% if tuto.category == dog %> <%= image_tag 'dog.jpg' %> <% end %>
像那样吗?
史蒂夫。
发布于 2016-09-20 14:21:03
另一篇文章说使用控制器和表单来创建新的类别。如果使用脚手架创建模型类别,则控制器中将包含new & create操作,以及new.html.erb和index.html.erb (如果使用该操作的话,则为or.slim)。
您可以在其中创建您的类别,然后在您的Tuto模型中选择一个。
讲得通?
史蒂夫。
https://stackoverflow.com/questions/39596365
复制相似问题