Topic: Help - I'm stuck

Help me someone! Argh!
I want to be able to store a hash in the database.

Say I have a Form. It has lotsa lines numbered #1, 2, ..., n

Now, I want to be able to access these fields in the view like so:

form.lines[1]

And in the controller:
lines[52] = lines[12] + lines[32]

How should I declare them in the model?

I tried, in my model Form.rb:

  attr_accessible :local_lines

  def local_lines
    self.local_lines ||= Hash.new
  end

  def local_lines(lines)
    self.local_lines = lines
  end


And in my view I try to access it:
  <%= @form.local_lines[1]  %>

but I get this error:
wrong number of arguments (0 for 1)

Now, I MIGHT be doing something wrong, so don't blame Rails.

"Iwana ga hana"

Re: Help - I'm stuck

1) storing a hash to your database - http://railsforum.com/viewtopic.php?pid=52630#p52630
Just replace the arrays with hashes.
2) Looks like you are creating getter and setter methods for local_lines, but really you don't have to, that is what attr_accessible does :), so in your initialize method of the model you can set the default for local_lines like this:

attr_accessible :local_lines

def initialize(*args)
  @local_lines = Hash.new  # default local lines to a hash
  super(*args)
end


3) Your implementation looks like an array though?  indexes relate to arrays and keys relate to hashes.

Re: Help - I'm stuck

Thanks for your reply.

1. To answer your 3) - wow I guess I really COULD use an array. I am still in c# world.
The reason I wanted hashes is because I wanted to do things like:
if line[1] == null
  //dont use it
end

And I also wanted to set line[10] withouth having lines 0...9 defined!
But I guess ruby can do ALL that with arrays.

2. Now - does that change the way I would store it in db, if I were to use arrays? Or do I still do that yaml stuff..

Thanks again,

pullmonkey wrote:

1) storing a hash to your database - http://railsforum.com/viewtopic.php?pid=52630#p52630
Just replace the arrays with hashes.
2) Looks like you are creating getter and setter methods for local_lines, but really you don't have to, that is what attr_accessible does smile, so in your initialize method of the model you can set the default for local_lines like this:

attr_accessible :local_lines

def initialize(*args)
  @local_lines = Hash.new  # default local lines to a hash
  super(*args)
end


3) Your implementation looks like an array though?  indexes relate to arrays and keys relate to hashes.

"Iwana ga hana"

Re: Help - I'm stuck

The example, I pointed to used arrays.  So, yes it will work for you.

Re: Help - I'm stuck

I decided to store lines in a Lines table.
Now I have a little problem. I don't know how to update the lines directly.
For example I have models, controller:

# Line model
class Line < ActiveRecord::Base
  belongs_to :form
end
# Form model
class Form < ActiveRecord::Base
  has_many :lines
end

# Controller
class WorksheetController < ApplicationController
  def index
    @form = Form.find_or_create_by_name('worksheet')
    if(@form.lines.count == 0)
      40.times { @form.lines.build }
    end
    @form.save
  end

  def update
    @form = Form.find_by_name('worksheet')
    @form.lines[1].value = params[:n1]
    @form.lines.each do |line|
      line.save
    end
    @form.save
    redirect_to :action=>'index'
  end
end


This is what I have in my view
# The view
<h1>Worksheet</h1>

  <% form_for :form, @form ,:url=> {:action=>'update'}  do |f| %>
   
    <%= text_field_tag :n1, @form.lines[1].value %>

    <%= submit_tag "Save" %>
  <% end %>


But I CANNOT do directly this in my View for some reason:
  <% form_for :form, @form ,:url=> {:action=>'update'}  do |f| %>
   
    <%= f.text_field @form.lines[1].value %>

    <%= submit_tag "Save" %>
  <% end %>

Last edited by drozzy (2008-03-07 15:16:19)

"Iwana ga hana"

Re: Help - I'm stuck

Take a look at fields_for (see the example) - http://api.rubyonrails.org/classes/Acti … ml#M000921

Re: Help - I'm stuck

Thanks, but I already look at it before, and it doesn't seem to solve my problem.

What fields_for do(as i understand) is just iterate over all lines[] in my array say,
but I actually want to access lines[i] etc...Whatever, I give up.. Going to go back to winforms...

Thanks for all your help though.

pullmonkey wrote:

Take a look at fields_for (see the example) - http://api.rubyonrails.org/classes/Acti … ml#M000921

"Iwana ga hana"

Re: Help - I'm stuck

I think it is exactly what you want - watch the complex forms 1-3 videos at http://railscasts.com
You want to use something like this, but f is the form object, and has nothing to do with the line object:

<%= f.text_field @form.lines[1].value %>

Instead you would do something like this:
<% fields_for @form.lines do |line_field| %>
  <%= line_field.text_field :value %>
<% end %>

After that take a look at the params that are submitted and change your update method accordingly.

Last edited by pullmonkey (2008-03-09 12:37:53)