# Basics questions on rails

**URL:** <https://www.ruby-forum.com/t/basics-questions-on-rails/147401>\
**Category:** Rails\
**Created:** [September 1, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/basics-questions-on-rails/147401 "2008-09-01T14:16:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [September 1, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/basics-questions-on-rails/147401/1 "2008-09-01T14:16:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [September 1, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/basics-questions-on-rails/147401/2 "2008-09-01T14:23:06Z")

</div>

On 1 Sep 2008, at 13:16, Vincent Pérès wrote:

> 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

> 1. 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)

> 1. 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

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [September 1, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/basics-questions-on-rails/147401/3 "2008-09-01T16:02:17Z")

</div>

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 🙂

Marcelo.

On Mon, Sep 1, 2008 at 9:22 AM, Frederick C.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [September 2, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/basics-questions-on-rails/147401/4 "2008-09-02T12:11:47Z")

</div>

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 :  
[Peak Obsession](http://api.rubyonrails.com/classes/ActiveResource/Errors.html)

Cheers,  
Vincent
