我想在我的应用中创建类别: 我受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
非常感谢你的帮助
答案 0 :(得分:1)
另一篇文章称使用控制器和表单来创建新类别。如果您使用scaffold创建模型类别,那么您将拥有new
&amp;控制器中的create
操作以及new.html.erb
和index.html.erb
(如果您正在使用该操作,则为{s}}。
您可以在那里创建类别,然后在Tuto模型中选择一个。
有意义吗?
史蒂夫。
答案 1 :(得分:1)
将图片存储在资产中。在表单/视图中使用条件?:
<% if tuto.category == dog %>
<%= image_tag 'dog.jpg' %>
<% end %>
那样的东西?
史蒂夫。