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") %>