2014-09-21 106 views
0

該場景是我創建一個食譜應用程序。如果你有你的初始配方模型,並且因爲你可以有多種成分,我有一個單獨的成分模型,我試圖嵌套在配方表單中。未定義的方法`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>

+0

什麼是「耙路徑」命令的輸出 – 2014-09-22 00:05:06

+0

我得到了: 耙子中止了! 不知道如何構建任務「路徑」 - 我在我的應用程序根目錄中鍵入'rake path'之前從未使用該命令。 @AntarrByrd – BDeGiglio 2014-09-22 01:15:36

+0

對不起,我的意思是耙路線 – 2014-09-22 04:07:31

回答

0

作用域

首先,您的範圍語法不正確:

#app/models/recipe.rb 
class Recipe < ActiveRecord::Base 
    belongs_to :catagory #-> should always be lower case 
    has_many :ingredients 
    accepts_nested_attributes_for :ingredients 

    scope :visible,  -> { where(:visible => true) } 
    scope :invisible, -> { where(:visible => false) } 
    scope :sorted,  -> { order("recipes.position ASC") } 
    scope :newest_first, -> { order("recipes.created_at DESC")} 
    scope :search, ->(query) { where(["name LIKE ?", "%#{query}%"])} 
end 

你會使用scopes documentation確定語法


更好

表格

其次,你的表格&支持controller代碼不正確

要影響嵌套表格,你最好做以下幾點:

#app/controllers/recipes_controller.rb 
class RecipesController < ApplicationController 
    def new 
     @recipe = Recipe.new 
     @recipe.ingredients.build 
    end 

    def create 
     @recipe = Recipe.new recipe_params 
    end 

    private 

    def recipe_params 
     params.require(:recipe).permit(:name, :position, ingredients_attributes: [:name]) 
    end 
end 

#app/views/recipes/new.html.erb 
<%= form_for @recipe do |f| %> 
    <%= f.text_field(:name) %> 
    <%= f.select(:position, [email protected]_count) %> 

    <%= f.fields_for :ingredients do |i| %> 
     <%= i.text_field :name %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

這會給你傳遞屬性的能力你需要通過「父母」的形式。你的問題是,你要在表單中嵌入表單

您將看到如何實現你要找的內容與this documentation


修復

您將只能使用單一表格來解決您的問題

路徑錯誤將來自您的嵌入表單(對於@ingredient),並且可能會通過刪除引用來解決從這個角度來看(如上)

+0

我已經更正了代碼,因爲您已經描述過它,就好像它不識別@recipe,因爲我現在收到此錯誤:未定義的方法'recipes_path'for# <#<類別:0x007fcbf799ba90>:0x007fcbf799a7f8> – BDeGiglio 2014-10-05 16:46:13

相關問題