私はRailsアプリに取り組んでおり、chef
モデルとモデルの2つのモデルがありdish
ます。
class Dish < ActiveRecord::Base
belongs_to :chef
attr_accessible :description, :photo, :price
validates :chef_id, presence: true
has_attached_file :photo
end
class Chef < ActiveRecord::Base
attr_accessible :name, :email, :mobile ,:password, :password_confirmation, :postcode
has_many :dishes
has_secure_password
end
私(シェフ)は、/uploadurlにアクセスして料理を作成しようとしています。
<%= form_for(@dish) do |d| %>
<%= d.label :description, "Please name your dish..."%>
<%= d.text_field(:description)%>
<%= d.label :price, "What should the price of the dish be..."%>
<%= d.number_field(:price)%>
<%= d.submit "Submit this Dish", class: "btn btn-large btn-primary"%>
<% end %>
作成した料理をシェフのショーページに表示したいのですが、
<% provide(:title, @chef.name)%>
<div class = "row">
<aside class = "span4">
<h1><%= @chef.name %></h1>
<h2><%= @chef.dishes%></h2>
</aside>
<div>
<% end %>
そして、dishes_controller
は:
class DishesController < ApplicationController
def create
@dish = chef.dishes.build(params[:dish])
if @dish.save
redirect_to chef_path(@chef)
else
render 'static_pages/home'
end
しかし、/ upload urlからディッシュを作成しようとすると、dish_controllerで次のエラーが発生します。
NameError undefined local variable or method `chef' for #<DishesController:0x3465494>
app/controllers/dishes_controller.rb:5:in `create'
すべての変数をインスタンス化したと思いますが、問題は解決しません。