カテゴリ名に従って画像を表示したい。私の画像は、app/assets/images
にあります。私の作品は、私がコンソールに作成したカテゴリを持っています。 今、このエラーがあります(もう一度...)。 貴重なご支援をいただき、ありがとうございます。ここrails 4未定義のメソッド 'name' for nil:NilClass - カテゴリ
は私のフォームです:
= 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 a category"}
= f.input :content, as: :text, input_html: { rows: "15" }
= f.button :submit, "Save"
tutos_controller.rb
class TutosController < ApplicationController
before_action :authenticate_user!
before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]
def index
@tutos = Tuto.all.includes(:user, :category)
end
def show
@tuto = Tuto.find(params[:id])
@user = User.all
end
def new
@tuto = Tuto.new
end
def edit
end
def create
@tuto = Tuto.new(tuto_params)
@tuto.user_id = current_user.id
respond_to do |format|
if @tuto.save
flash[:success] = "Test"
format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
format.json { render :show, status: :created, location: @tuto }
else
format.html { render :new }
format.json { render json: @tuto.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @tuto.update(tuto_params)
format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
format.json { render :show, status: :ok, location: @tuto }
else
format.html { render :edit }
format.json { render json: @tuto.errors, status: :unprocessable_entity }
end
end
end
def destroy
@tuto.destroy
respond_to do |format|
format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
format.json { head :no_content }
end
end
def upvote
@tuto.upvote_by current_user
redirect_to :back
end
private
# def get_user
# @user = User.find(@tuto.user_id)
# end
def set_tuto
@tuto = Tuto.find(params[:id])
end
def tuto_params
params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id)
end
end
categories_controller.rb
class CategoriesController < ApplicationController
private
def categories_params
params.require(:categories).permit(:name, :description, :id)
end
end
私の見解/ TUTOS/index.html.slim
.container
.row
.col-xs-12.col-sm-12
h1.text-gray Tutorials
br
-if user_signed_in?
= link_to "Create a tuto", new_tuto_path, class:"btn btn-success"
#tutos.transitions-enabled
- @tutos.each do |tuto|
.box.panel-default
-if tuto.category.name == "Ruby"
=image_tag("select/ruby.png")
-elsif tuto.category.name == "Rails 4"
=image_tag("selec/rails4.png")
-elsif tuto.category.name == "Rails 5"
=image_tag("selec/rails5.png")
-elsif tuto.category.name == "Heroku"
=image_tag("select/heroku.png")
-elsif tuto.category.name == "AWS"
=image_tag("select/aws.png")
= link_to tuto.title, tuto_path(tuto)
h6 Created by:
= tuto.user.full_name
hr
ここで私の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
EDITはエラーメッセージです:あなたはいくつかの点でCATEGORY_IDことなく、少なくとも1つのTutoオブジェクトを作成している可能性があり、それはあなたの(まだだよう
どのようなエラーが発生するのは?それはあなたに行番号を与えますか? – eeeeeean
私の編集を参照してください:) –