該場景是我創建一個食譜應用程序。如果你有你的初始配方模型,並且因爲你可以有多種成分,我有一個單獨的成分模型,我試圖嵌套在配方表單中。未定義的方法`ingredients_path'爲#<#<Class:0x007fdbfd71c3d0>:0x007fdbf94bea60>
class Recipe < ActiveRecord::Base
belongs_to :Catagory
has_many :ingredients
accepts_nested_attributes_for :ingredients
scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("recipes.position ASC") }
scope :newest_first, lambda { order("recipes.created_at DESC")}
scope :search, lambda {|query|
where(["name LIKE ?", "%#{query}%"])
}
end
成分模型:
class Ingredient < ActiveRecord::Base
belongs_to :recipe
end
規則控制器:
class RecipeController < ApplicationController
def index
@recipes = Recipe.sorted
end
def new
@recipe = Recipe.new({:name => "Default"})
@recipe_count = Recipe.count + 1
@ingredient = Ingredient.new({:name => "Enter Ingredient"})
end
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
flash[:notice] = 'You have successfully created a new recipe!'
redirect_to(:action => 'index')
else
@recipe_count = Recipe.count + 1
render('new')
end
end
def edit
@recipe = Recipe.find(params[:id])
@recipe_count = Recipe.count
end
def update
@recipe = Recipe.find(params[:id])
if @recipe.update_attributes(recipe_params)
flash[:notice] = 'Your changes have been saved!'
redirect_to(:action => 'index')
else
@recipe_count = Recipe.count
render('edit')
end
end
def delete
@recipe = Recipe.find(params[:id])
end
def destroy
@recipe = Recipe.find(params[:id]).destroy
flash[:notice] = 'Your recipe has been deleted'
redirect_to(:action => 'index')
end
private
def recipe_params
params.require(:recipe).permit(:name, :position, :ingredient, :directions, :created_at)
end
end
配方形式:
<table summary="Recipe form fields">
<tr>
<th><%= f.label(:name, "Recipe Name") %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:position) %></th>
<td><%= f.select(:position, [email protected]_count) %></td>
</tr>
<%= form_for @ingredient do |f| %>
<th><%= f.label(:position) %></th>
<td><%= f.text_field :name %></td>
<% end %>
<tr>
<th><%= f.label(:directions) %></th>
<td><%= text_area_tag(:message, "Be specific", size: "50x30") %></td>
</tr>
</table>
和我收到此錯誤: 未定義的方法`ingredients_path」 F或#<#:0x007fdbf94bea60>
什麼是「耙路徑」命令的輸出 – 2014-09-22 00:05:06
我得到了: 耙子中止了! 不知道如何構建任務「路徑」 - 我在我的應用程序根目錄中鍵入'rake path'之前從未使用該命令。 @AntarrByrd – BDeGiglio 2014-09-22 01:15:36
對不起,我的意思是耙路線 – 2014-09-22 04:07:31