Solving a problem: keep tracking of an index through controller & view

Hi,

I want some help to solve this problem that I have:

I have a form for some controller/model called Expense(s) in the
new_daily method, in that form I have a table with 5 rows, where I
have to put the fields needed to create each of daily expenses that I
could have, but of course, you would have more than 5 expenses in one
day, so I have a link to “Add Expense” to add a new row to the
expenses table form with the correct fields and internally it must
have a certain id, name structure wich is the same for all rows except
for and index, for example:

expenses[e1][name], expenses[e1][amount], expenses[e1][category]
expenses[e2][name], expenses[e2][amount], expenses[e2][category]

The problem is that I don’t know how to keep tracking of that index
for the form, I want to use a controller action “add_expense” that
calls a “add_expense.js.erb” to use jquery and rendering a partial
containing that new row, but with the correct index.

If I save the index in the add_expense controller method it gets reset
anytime I try to use the method again, and inside it I’ve defined like
@n ||= 5”
but it’s always 5, It doesn’t remember the “<%= @n += 1 %>” that I use
in the js view

Another option would be use unobstrusive javascript and get that
number in a data-numer attribute in that form table, but then I’m not
able to pass as a parameter to the partial that I want to render.

How would you solve this?

Greetings

On 16 February 2011 16:06, Felipe E. Castillo
[email protected] wrote:

have a certain id, name structure wich is the same for all rows except
for and index, for example:

Have you seen

I suggest you spend an hour working through it and the next episode
(part 2 oddly enough) in order to appreciate what is going on. I
think it may give you some ideas on the way to go.

Colin

thanks, that was my problem, I will try to adapt that episode to rails
3,
but the essence of the problem was the same.