user1466717
user1466717

Reputation: 789

HAML and mistake in form_for

I'm learning HAML and I have a problem with from_for

%b New Advert!

- form_for @car, :url => { :action => "create" } do |f|
  = f.text_field :body 
  = f.submit t('advert.form.save')

My Controller advert_controller

class AdvertController < ApplicationController
  def new
    @car = Car.new
  end

  def create
    @car = Car.new(params[:car])
  end
end

And my model car.rb

class Car < ActiveRecord::Base
  attr_accessible :title, :body
end

But I have a mistake:

undefined method `body' for #

NoMethodError in Advert#new

Showing /my_project/app/views/advert/new.html.haml where line #4 raised

UPD form_for with '=' doesn't work

= form_for @car, :url => { :action => "create" } do |f|
      = f.text_field :body 
      = f.submit t('advert.form.save')

Upvotes: 0

Views: 1897

Answers (1)

pjam
pjam

Reputation: 6356

I can see two errors here : first you should use = instead of - for form_form, indeed = will output the result whereas - won't.

As form_for returns HTML for the form, you want it to be written on the page ;)

As for the cause of your error, well I'm not entirely sure, but it seems that your Car model doeesn't have a body field and so it does not have a body method

Upvotes: 4

Related Questions