0

私は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'

すべての変数をインスタンス化したと思いますが、問題は解決しません。

4

1 に答える 1

1

この行で:

@dish = chef.dishes.build(params[:dish])

chef変数はインスタンス化されていません。次のようなことをしなければなりません:

@chef = Chef.find(params[:chef_id])
@dish = @chef.dishes.build(params[:dish])

このようにして、使用する前に @chef 変数が設定されます。

于 2012-08-16T14:11:12.293 に答える