A newbie question

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

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
[email protected]

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

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. [email protected]

Thank you very much for the messages.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs