Select and file_field

Hi

Let’s suppose I have a form in views/users/new.html.erb, and:

  1. :first_name field has validates :presence => true
  2. :gender in mysql database field can have boolean values (0-Female,
    1-Male)
  3. :avatar has a binary field in mysql database

<%= form_for(@user) do |f| %>

<%= f.text_field :first_name %>

<%= f.text_field :second_name %>

…else fields which need to validate
<%= f.select :gender, [[‘Select’,nil],[‘Male’, 1],[‘Female’, 0]] %>
<%= f.file_field :avatar %>
<%= f.submit “Add” %>

<% end %>

  1. When I am not put anything to first_name field, but fill in 3
    others(second_name, select and photo), I see error message and
    second_name still filled in, but avatar and gender field is empty now.
    What’s matter?

  2. Assume I load image avatar to database. How I can load it from db?

Misha O. wrote in post #1008068:

<%= form_for(@user) do |f| %>

<%= f.text_field :first_name %>

<%= f.text_field :second_name %>

…else fields which need to validate
<%= f.select :gender, [[‘Select’,nil],[‘Male’, 1],[‘Female’, 0]] %>
<%= f.file_field :avatar %>
<%= f.submit “Add” %>

<% end %>

  1. When I am not put anything to first_name field, but fill in 3
    others(second_name, select and photo), I see error message and
    second_name still filled in, but avatar and gender field is empty now.
    What’s matter?

select(object, method, choices, options = {}, html_options = {})

Shouldn’t the choices for your select use something like:

options_for_select([[“Dollar”, “$”], [“Kroner”, “DKK”]])
Dollar\nKroner

Instead of just the Array of Arrays?

AFAIK file fields don’t restore the state from one request to the next.
So the user will have to reselect the file in case errors occur in the
initial request. Note: I could be mistaken on this.

  1. Assume I load image avatar to database. How I can load it from db?

Have you looked at the Paperclip gem. It should make dealing with file
uploads easier.

https://github.com/thoughtbot/paperclip

  1. With select: the right values are:
    <%= f.select :gender, [[‘Select’,nil],[‘Male’, true],[‘Female’, false]] %>

With file field - I not found decision yet.

  1. I don’t want load files to my filesystem. I want load them to
    database. What I must do?