basics questions on rails

Hello,

I started to use rails few weeks ago, and I have got basic question
about it.

1. There is a way to keep my form filled, after submit it (for example
if I get errors) ?

2. I would like to create a 'tree' with my datas like 'pages' for
example : a page has one children and belongs to a page. I added has_one
and belongs_to to the model but it doesn't work.

3. My last question is about the models test. For example :
validates_presence_of :name, :price, :description, :message =>
'required'
I would like to match this message in my controller to display it in a
flash after...

Can you help me for these points please !

Cheers,
Vincent

Hello,

I started to use rails few weeks ago, and I have got basic question
about it.

1. There is a way to keep my form filled, after submit it (for example
if I get errors) ?

typically this is done by rendering the form. This is the common
if @foo.save
  redirect_to ...
else
  render ...
end

pattern which you should be able to find many examples off

2. I would like to create a 'tree' with my datas like 'pages' for
example : a page has one children and belongs to a page. I added
has_one
and belongs_to to the model but it doesn't work.

'it doesn't work' is too vague to be able to offer any real advice.
You'll need to play with the class_name and foreign_key options (or
possible have a look at acts_as_tree)

3. My last question is about the models test. For example :
validates_presence_of :name, :price, :description, :message =>
'required'
I would like to match this message in my controller to display it in a
flash after...

Assuming your record is @record then @record.errors contains
information about the failed validations (eg
@record.errors.full_messages. It can also do stuff like only give you
errors on certains fields etc...)

Fred

Hi.

If you assign the instance variable of your model to the view and use a
form helper to build your forms, Rails will automatically assign a
value if it is present in the instance object’s hash:

actionpack-2.1.0/lib/action_view/helpers/form_helper.rb, line 568:

(…)

=> 568 content_tag(“textarea”, html_escape(options.delete(‘value’) || value_before_type_cast(object)), options)

(…)

The call to content tag tries to extract the value from the object or deletes the tag if it is not present.

Where object is the object (model’s instance) that has been passed to the form_for helper method.

So, just follow Frederick’s advice and you should be good to go :slight_smile:

Marcelo.

Hello,

Thanks for your quick answers !

1. I gave @record to my form_for and it was filled automatically.

2. I will play again with belongs_to, has_one... I think it is basic
configuration (page has a parent page id on each record)

3. Perfect ! I found it thanks to your answer :
http://api.rubyonrails.com/classes/ActiveResource/Errors.html

Cheers,
Vincent