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? %> <div id="error_explanation"> <h2><%= pluralize(@switch.errors.count, "error") %> prohibited this switch from being saved:</h2> <ul> <% @switch.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <%= f.fields_for :configuration do |c| %> <%= c.text_field :snmpCommunity %> <%= c.text_field :ipAddress %> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-14 21:38 UTC
Re: Is there a serialization Example for Rails 3.0?
On 14 Apr 2011, at 19:30, Andrew Miehs <andrew-64B0ZDxNVfizQB+pC5nmwQ@public.gmane.org> wrote:> 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? >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> 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? %> > <div id="error_explanation"> > <h2><%= pluralize(@switch.errors.count, "error") %> prohibited this switch from being saved:</h2> > > <ul> > <% @switch.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > > <div class="field"> > <%= f.label :name %><br /> > <%= f.text_field :name %> > </div> > > > <%= f.fields_for :configuration do |c| %> > <%= c.text_field :snmpCommunity %> > <%= c.text_field :ipAddress %> > <% end %> > > > <div class="actions"> > <%= f.submit %> > </div> > <% end %> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Fred, On 14/04/2011, at 11:38 PM, Frederick Cheung 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 flyI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.