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')
I can see two errors here :
first you should use
=instead of-forform_form, indeed=will output the result whereas-won’t.As
form_forreturns 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
Carmodel doeesn’t have abodyfield and so it does not have abodymethod