为什么我在Ruby中得到NoM​​ethodError?

时间:2016-09-04 16:20:00

标签: ruby-on-rails web-applications nomethoderror actioncontroller

我目前正在研究配方盒应用程序,在轨道上使用ruby。当我想创建一个新配方它说

  

未定义的方法`title'

= f.input :title, input_html: { class: 'form-control' }

这是我的 form.html.haml

= simple_form_for @recipe, html: { multipart: true } do |f|
    - if @recipe.errors.any?
        #errors
            %p
                = @recipe.errors.count
                Prevented this recipe from saving
            %ul
                - @recipe.errors.full_messages.each do |msg|
                    %li= msg
    .panel-body
        = f.input :title, input_html: { class: 'form-control' }
        = f.input :description, input_html: { class: 'form-control' }

    = f.button :submit, class: "btn btn-primary"

这是我的 recipes_controller.rb

class RecipesController < ApplicationController
before_action :find_recipe, only: [:show, :edit, :update, :destroy]

def index
end

def show
end

def new
    @recipe = Recipe.new
end

def create
    @recipe = Recipe.new(recipe_params)

    if @recipe.save 
        redirect_to @recipe, notice: "Toll! Neues Rezept erfolgreich erstellt."
    else
        render 'new'
    end
end

private

def recipe_params
    params.require(:recipe).permit(:title, :description)
end

def find_recipe
    @recipe = Recipe.find(params[:id])
end

1 个答案:

答案 0 :(得分:1)

  1. 您必须在“新”字样中呈现表单。查看
  2. 你必须有专栏&#39; title&#39;在您的数据库中
  3. 向我们展示&#39; debug @ recipe&#39;打印,是否有标题&#39;属性?