ruby on rails で初心者向けのブログ投稿、カテゴリ選択できるwebアプリを作っています。
ローカルホストでhttp://localhost:3000/categories/newを表示しようとしたところ、次のエラー分が出ました。
エラー文
app/views/categories/new.html.erb where line #5 raised:
undefined method `name' for #<Category id: nil, created_at: nil, updated_at: nil>
ソースコード
ruby
1app/views/categories/new.html.erb 2<h1>New Category</h1> 3<%= form_for(@category) do |f| %> 4 <div class="field"> 5 <%= f.label :name %><br> 6 <%= f.text_field :name %> 7 </div> 8 <div class="actions"> 9 <%= f.submit %> 10 </div> 11<% end %> 12 13app/controllers/categories_controller.rb 14class CategoriesController < ApplicationController 15 def index 16 @categories = Category.all 17 end 18 def show 19 @category = Category.find(params[:id]) 20 end 21 def create 22 category_params = params.require(:category).permit(:name) 23 @category = Category.new(category_params) 24 25 if @category.save 26 redirect_to @category, notice: 'Category was successfully created' 27 else 28 render :new 29 end 30 end 31 def new 32 @category = Category.new 33 end 34end 35 36app/models/category.rb 37class Category < ActiveRecord::Base 38 has_many :posts 39end
これってnameが定義されてないってことですよね?
かなり初歩的な質問ですが、nameはどこで定義すればよいのでしょうか?
ご教授いただきたいです。
回答2件
あなたの回答
tips
プレビュー