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 %>
  <table class="form">
  <tr>
  <td class="label">Nome:</td>
  <td class="field"><%= text_field :account, :name %></td>
  </tr>
  <tr>
  <td class="label">Banco:</td>
  <td class="field"><%= select("account", "bank_id",
Bank.find(:all).map {|b| [b.name, b.id]}) %></td>
  </tr>
  <tr>
  <td class="label">Número:</td>
  <td class="field"><%= text_field :account, :number %></td>
  </tr>
  <tr>
  <td class="label">Saldo inicial:</td>
  <td class="field"><%= text_field :account, :initial_balance %></td>
  </tr>
  <tr>
  <td class="label">Moeda:</td>
  <td class="field"><%= select("account", "currency_id",
Currency.find(:all).map {|c| [c.name, c.id]}) %></td>
  </tr>
  <tr>
  <td class="label">Comentários:</td>
  <td class="field"><%= text_field :account, :comment %></td>
  </tr>
  <tr>
  <td colspan="2" class="label"><%= submit_tag "OK" %></td>
  </tr>
  </table>
<% 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 Hora

Hi Gabriel,

Could please post the problem that you are getting ?

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.

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.

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

Gabriel Hora 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:

  <td class="field"><%= text_field :account, :name %></td>

<%= f.text_field :name %>

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

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

  <td class="field"><%= text_field :account, :number %></td>

<%= f.text_field :number %>

  <td class="field"><%= text_field :account, :initial_balance %></td>

<%= f.text_field :initial_balance %>

  <td class="field"><%= text_field :account, :comment %></td>

<%= f.text_field :comment %>

  <td colspan="2" class="label"><%= submit_tag "OK" %></td>

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

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.

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.