2014-12-04 37 views
-3

我的應用程序使用的是Rails 4.1.8,我使用簡單表單gem。 我在private下強參數定義中得到了未定義的局部變量或方法錯誤,我不明白原因。Ruby on Rails - 未定義的局部變量或方法(強參數)

_form.haml.html

= simple_form_for @recipe, html: { multipart: true } do |f| 
    - if @recipe.errors.any? 
     #errors 
      %h2 
       = pluralize(@recipe.errors.count, "error") 
       prohibited this recipe from being saved 
      %ul 
       - @recipe.errors.full_messages.each do |msg| 
        %li= msg 

    = f.input :title 
    = f.input :serving 
    = f.input :prep 
    = f.input :cook 


    = f.button :submit 

控制器

class RecipesController < ApplicationController 

def create 
    @recipe = Recipe.new(recipe_params) 
    if @recipe.save 
     redirect_to @recipe, notice: "Successfully created" 
    else 
     render 'new' 
    end 

    private 

    def recipe_params 
     params.require(:recipe).permit(:title, :serving, :prep, :cook) 
    end 

    end 
end 

enter image description here

而且提交按鈕帶我到指數路徑http://localhost:3000/recipes而不是 顯示http://localhost:3000/recipes/1

+0

我是新來的鐵軌,我相信這種事情可能會發生。我會盡可能地關閉/刪除這個問題。 – 2014-12-04 10:52:31

回答

2

您沒有正確關閉create方法。它應該是:

class RecipesController < ApplicationController 
    def create 
    @recipe = Recipe.new(recipe_params) 
    if @recipe.save 
     redirect_to @recipe, notice: "Successfully created" 
    else 
     render 'new' 
    end 
    end 

    private 

    def recipe_params 
    params.require(:recipe).permit(:title, :serving, :prep, :cook) 
    end 
end 
+0

謝謝。我是新來的鐵軌,我認爲'如果else'不需要'end',我也用'end'來關閉私有。 – 2014-12-04 10:45:40

+0

@SeongLee'if else'需要'end'(除非是單行'if')。 '私人'不需要'結束'。 – 2014-12-04 10:49:55

相關問題