Whats wrong with my form? (rails 2.0)

Hi everyone,

I’m testing the 2.0 rails (after running into some gem problems to
upgrade it) and I really don’t know what’s wrong with this form. I
tried to keep it as simple as possible (maybe too simple that I forgot
something?).
Can someone point me the problem here?

The view:

<% form_for :account do %>

Nome: <%= text_field :account, :name %>
Banco: <%= select("account", "bank_id", Bank.find(:all).map {|b| [b.name, b.id]}) %>
Número: <%= text_field :account, :number %>
Saldo inicial: <%= text_field :account, :initial_balance %>
Moeda: <%= select("account", "currency_id", Currency.find(:all).map {|c| [c.name, c.id]}) %>
Comentários: <%= text_field :account, :comment %>
<%= submit_tag "OK" %>
<% end %>

The action:

def new
@account = Account.new(params[:account])
if request.post?
@account.balance = @account.initial_balance
if @account.save
redirect_to :action => “list”
else
redirect_to :action => “new”
end
end
end

I would appreciate some help here.

Thank you,

Gabriel H.

Hi Gabriel,

Could please post the problem that you are getting ?

On Tue, Mar 11, 2008 at 8:28 AM, Gabriel H. [email protected]
wrote:

<%= select("account", "currency_id", <% end %> redirect_to :action => "new" Gabriel H. >


Rui

“Rubi? Aquela novela do SBT?”
~ Carla Perez sobre Ruby

“Em Python, tudo é objeto, além de lindo e maravilhoso.”
~ Caetano Veloso sobre Python

“SDD → Skol Driven Development is the new age of computing.”
~ Martin F.

Oh, sorry, the problem is it’s not saving the data.
With rails 1.2.4 (using the form_tag instead) it worked, now its not
saving.

I figure its something wrong I’m doing in the view cuz the action is
quite simple right?

And I’m new to Rails so I don’t know where to look for system error
messages for the problem.

in your app in the log folder you’ll find the file development.log

with rake log:clear you can clear that (can’t damage anything)

after every call to controller/action you’ll get the whole set of
params used for that call and all error messages in that file

check if the view hands in any data as params

with:
logger.info “text #{vars}”

you can write debug messages directly to that file from your controller

We still need way more information than what you’re giving us.

Is it erroring when it saves? What URL is it generating? Is it even
going to
the create/update page?

The more information you give us, the greater the chance of us being
able to
solve your problem.

I found the problem.

I’m from Brazil so we write numbers like this 15.000,00 (comma as
decimal separator)…
I was inputing like this in the form and it didn’t work, when I
changed to this 15000.00 it worked.
But, there was nothing on the log giving me any tips for that…no SQL
error messages, no nothing…

This is the entry on the log…between the BEGIN the COMMIT there’s
nothing…as if there was no SQL to execute.
That’s kinda weird, right? Or is it the right behavior?

Processing AccountsController#new (for 127.0.0.1 at 2008-03-11
09:31:58) [POST]
Session ID:
BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo
%0ASGFzaHsABjoKQHVzZWR7AA%3D
%3D–9e3a490edfbb816e5ed5e0d1573fbdd514229823
Parameters: {“commit”=>“OK”, “account”=>{“name”=>“Nova conta”,
“number”=>“5218 211455”, “currency_id”=>“1”,
“initial_balance”=>“1520,00”, “bank_id”=>“1”, “comment”=>“Sem
comentários”}, “action”=>“new”, “controller”=>“accounts”}
Account Columns (0.002612) SHOW FIELDS FROM accounts
SQL (0.000210) BEGIN
SQL (0.000146) COMMIT
Redirected to http://localhost:3000/accounts/new
Completed in 0.03512 (28 reqs/sec) | DB: 0.00297 (8%) | 302 Found
[http://localhost/accounts/new]

And one last thing, how do I change this number verification? I need
to “def validate” it myself?
or the validates_numericality_of works only for US like formating?

Thanks for the help.

On Mar 11, 9:22 am, Mark B. [email protected]

Gabriel H. wrote:

<% form_for :account do %>

#form_for will scope the account object saving you from referring to it
in every entry. Use:

<% form_for(@account) do |f| %>

Then the entries become:

<%= text_field :account, :name %>

<%= f.text_field :name %>

<%= select("account", "bank_id", Bank.find(:all).map {|b| [b.name, b.id]}) %>

I haven’t fully checked, but I don’t think #select is scoped, so leave
this as it is.

<%= text_field :account, :number %>

<%= f.text_field :number %>

<%= text_field :account, :initial_balance %>

<%= f.text_field :initial_balance %>

<%= text_field :account, :comment %>

<%= f.text_field :comment %>

<%= submit_tag "OK" %>

<%= f.submit “OK” %>

If this fails, send the errors from your log file along with the
parameters in the log being sent to your #new action.

You may have posted the wrong fragment, or misunderstood the intention
of the controller. The ‘new’ method (shown above) is intended only to
return a blank form that you can fill out. The ‘create’ method should
be used to save the data for the first time.

As for the formatting, you may need to use a :before_validation
callback to massage the numeric into the ‘correct’ form.