2016-09-20 6 views
0

作成: 私はthis question私は私のアプリでカテゴリを作成したいと思い、レール内のカテゴリのApp

に触発されてしまった。しかし@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 

おかげでたくさん

答えて

1

画像を資産に保存しましたか?そして、フォーム/ビューのような条件付き?:

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

何かを使うのか?

スティーブ。

+0

! –

1

は、他のポストは新しいカテゴリを作成するために、コントローラと、フォームを使用することを言います。 scaffoldを使用してモデルカテゴリを作成した場合は、コントローラーにはnew & createアクションがあり、使用している場合はnew.html.erbindex.html.erb(またはそれを使用していれば)です。

カテゴリをそこに作成し、Tutoモデルでカテゴリを選択することができます。

意味がありますか?

スティーブ。

+1

"Dogs、cats、birds"のようなデフォルトカテゴリが必要です –

+1

'Category'のインスタンスをハードコードし、' new'や 'create'アクションに何もないこと、あるいはそれらのアクションを管理者のみが作成したものだけがユーザに提供されます。実稼働環境のコンソールまたは管理ユーザーとして作成できます。 – OnlySteveH

+0

私はコンソールを知っています...しかし、私はそれが対応する画像で動作するかどうかはわかりません...このアイテムが選択されている場合、この画像を表示しています...あなたは見ていますか? それはコード内にある必要があります...しかし、私はどのようにわかりません: '( –

関連する問題