my model has four fields. So for the data entry I usually would do something like this
<%= form_for @user do |f| %>
<p>
<%= f.label :field1 %><br />
<%= f.text_field :field1 %>
</p>
<p>
<%= f.label :fi开发者_如何学运维eld2 %><br />
<%= f.text_field :field2 %>
</p>
<p>
<%= f.label :field3 %><br />
<%= f.text_field :field3 %>
</p>
<p>
<%= f.label :field4 %><br />
<%= f.text_field :field4 %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
I'd like to create a view with one text_area though, to enable mass data entry.
The idea would be to paste multiple lines each containing four fields separated by a comma into the text_area.
On submit I would then push the data into the DB.
How would a form like this look like?
Where would I the parse the content of the text_area and how would I save the data?
Thanks Thomas
P.S. While everyone is thinking what to reply, I wonder if a calculated column would be a solution here. I mean there is nothing to calculate here, but at least I could create a form with a field from the model and on post split the data to the other fields.
All you need is a virtual attribute. There is a very useful rails cast about there here.
Basically, you would add a a field in your model, say called mass_data
, and create getters and setters for it that convert to and from the mass_data
format to four separate fields corresponding to your model. Then, you can add a text field in the form for that attribute just as if it was a real attribute.
i.e, in your controller, do this:
def mass_data
[#{field1},#{field2},#{field3},#{field4}].join(',')
end
def mass_data=(data)
split=mass_data.split(',')
self.field1=split.first
...
self.field4=split.last
end
and then just add a text field to the form view:
<%= f.text_field :mass_data %>
精彩评论