2014-12-04 67 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

相关问题