Search code examples
ruby-on-railsweb-applicationsnomethoderroractioncontroller

Why am I getting a NoMethodError in Ruby?


I am currently working on an recipe box application, using ruby on rails. When I want to create a new recipe It says

undefined method `title'

for

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

This is my 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"

And this my 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

end


Solution

    1. You have to render form in 'new' view
    2. You have to have column 'title' in your DB

    Show us what 'debug @recipe' prints, is there 'title' attribute?