# A newbie question

**URL:** <https://www.ruby-forum.com/t/a-newbie-question/173002>\
**Category:** Rails\
**Created:** [July 31, 2009, 4:31am UTC](https://www.ruby-forum.com/t/a-newbie-question/173002 "2009-07-31T04:31:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 4:31am UTC](https://www.ruby-forum.com/t/a-newbie-question/173002/1 "2009-07-31T04:31:16Z")

</div>

I’m new with Rails and I’m creating a simple app.

I have these 3 files and would like to display an error message when  
the form fails, saying the problem. Something like ‘Title must be  
filled.’ .

I don’t know how to do that.

Please help me.

Thank you very much,

Gabriel.

* * *

# app\models\sfile.rb

class Sfile \< ActiveRecord::Base  
validates\_presence\_of :title  
end

# app\controllers\sfiles\_controller.rb

class SfilesController \< ApplicationController  
def index  
@page\_title = ‘Free SWF Hosting’  
@sfile = Sfile.new  
end

def create  
@sfile = Sfile.new(params[:sfile])  
respond\_to do |format|  
if @sfile.save  
flash[:notice] = ‘File was successfully created.’  
format.html { redirect\_to(:action =\> ‘index’) }  
else  
format.html { redirect\_to(:action =\> ‘index’) }  
end  
end  
end  
end

# app\views\sfiles\index.html.erb

Upload your file:

\<% form\_tag(:controller =\> “sfiles”, :action =\> “create”) do |f|%\>

\<%= label\_tag :title, 'FIle Name: ' %\>  
 \<%= text\_field\_tag :title %\>

\<%= label\_tag(:description, "File description (optional) :") %\>  
 \<%= text\_area\_tag(:description, nil, :size =\> "44x6") %\>  
 \<%= submit\_tag("Update") %\>

\<% end %\>
* * *

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 5:22am UTC](https://www.ruby-forum.com/t/a-newbie-question/173002/2 "2009-07-31T05:22:16Z")

</div>

Gabriel B. wrote:

> I’m new with Rails and I’m creating a simple app.
> 
> I have these 3 files and would like to display an error message when  
> the form fails, saying the problem. Something like ‘Title must be  
> filled.’ .

Look at errors\_on (or maybe it’s errors\_for).

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 6:17am UTC](https://www.ruby-forum.com/t/a-newbie-question/173002/3 "2009-07-31T06:17:57Z")

</div>

And one more thing. Your intended action is not index but new. Change  
action name to new and view file to new.html.erb

Sijo

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 7:56am UTC](https://www.ruby-forum.com/t/a-newbie-question/173002/4 "2009-07-31T07:56:41Z")

</div>

If I didn’t understand wrong.  
You need to add some validation into your “sfile” model  
like validates\_presence\_of :title, :on =\> :create, :message =\> “Title  
must be filled”

On Jul 31, 6:55 am, Gabriel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![archangel77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6a8cbe/32.png) [@archangel77](https://www.ruby-forum.com/u/archangel77)\
**Post date:** [July 31, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/a-newbie-question/173002/5 "2009-07-31T15:38:33Z")

</div>

Thank you very much for the messages.
