Is there a serialization Example for Rails 3.0?

Hi all,

I am new to ruby and rails coming from a perl background.

I was hoping someone could help point me to some sample code to
serialize data from a form.
My plan is to write a tiny app to generate Cisco switch configuration
files based on a template.

Below is what I have found so far based on what I could find.

Unfortunately the form does not automagically re-insert the values after
they are saved. I noticed other people commenting about the same
feature. Is there a better way of doing this?

How would I create an array of vlans under the variable configuration
using the form?
I am planning on adding an “add vlan” button which reloads the same
form, but I am not sure of the syntax.

Any pointers would be helpful.

Thanks in advance,

Regards

Andrew


=== switch.rb ===
class Switch < ActiveRecord::Base
serialize :configuration
end

=== _form.html.erb ===
<%= form_for(@switch) do |f| %>
<% if @switch.errors.any? %>


<%= pluralize(@switch.errors.count, “error”) %> prohibited
this switch from being saved:

  <ul>
  <% @switch.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :configuration do |c| %>
    <%= c.text_field :snmpCommunity %>
    <%= c.text_field :ipAddress %>
<% end %>
<%= f.submit %>
<% end %>

On 14 Apr 2011, at 19:30, Andrew M. [email protected] wrote:

You might try passing @switch.configuration, :configuration to
fields_for. Why use serialize though, rather than make those things
attributes of the switches table?

How would I create an array of vlans under the variable configuration using the
form?
I am planning on adding an “add vlan” button which reloads the same form, but I
am not sure of the syntax.

If vlans are a separate table (with an association with switches) then
accepts_nested_attributed makes this sort of thing pretty easy. I
believe there’s a railcard that covers one way of handling the client
side portion of adding new form thingies on the fly

Fred

Hi Fred,

On 14/04/2011, at 11:38 PM, Frederick C. wrote:

Unfortunately the form does not automagically re-insert the values after they
are saved. I noticed other people commenting about the same feature. Is there a
better way of doing this?

You might try passing @switch.configuration, :configuration to fields_for. Why
use serialize though, rather than make those things attributes of the switches
table?

I do not want to use attributes as there are not only 2 but about 20,
and additional ones may come at a later stage.
I am not quite sure what you mean by the above statement…

Changing
<%= f.fields_for :configuration do |c| %>
to
<%= f.fields_for @switch.configuration, :configuration do |c| %>
gives
undefined method `model_name’ for NilClass:Class

Unfortunately I am currently trying to work out how the rails framework
fits together while learning ruby syntax at the same time…

How would I create an array of vlans under the variable configuration using the
form?

I am planning on adding an “add vlan” button which reloads the same form, but I
am not sure of the syntax.

If vlans are a separate table (with an association with switches) then
accepts_nested_attributed makes this sort of thing pretty easy. I believe there’s
a railcard that covers one way of handling the client side portion of adding new
form thingies on the fly

I don’t actually want to do anything with this data other than running
it through a template. That is why I was looking at the following type
structure:

name = mySwitch
configuration =
{snmpCommunity = public},
{ipAddress = 10.0.0.1},
{value1 = x}
{vlan = [ { id = 1, name = Vlan1 }, { id = 2, name = Vlan2 }]}

etc etc…

I don’t really want to bother normalising the data as this structure may
change quite often…

I am trying to do this in Rails so that I get a chance to learn a little
about the framework…

Thanks for any comments.

Regards

Andrew

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