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 <p>Upload your file:</p> <% form_tag(:controller => "sfiles", :action => "create") do |f|%> <p> <%= label_tag :title, ''FIle Name: '' %><br /> <%= text_field_tag :title %> </p> <p> <%= label_tag(:description, "File description (optional) :") %><br /><%= text_area_tag(:description, nil, :size => "44x6") %><br /> <%= submit_tag("Update") %> </p> <% end %> -------------------------------
Gabriel Bianconi 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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 Bianconi <bianconi.gabr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.'' . > > 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 > <p>Upload your file:</p> > > <% form_tag(:controller => "sfiles", :action => "create") do |f|%> > > <p> > <%= label_tag :title, ''FIle Name: '' %><br /> > <%= text_field_tag :title %> > </p> > <p> > <%= label_tag(:description, "File description (optional) :") %><br / > > <%= text_area_tag(:description, nil, :size => "44x6") %><br /> > <%= submit_tag("Update") %> > </p> > <% end %> > > -------------------------------