Felipe Espinoza Castillo
2011-Feb-16 16:06 UTC
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 -- 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.
Colin Law
2011-Feb-16 16:54 UTC
Re: Solving a problem: keep tracking of an index through controller & view
On 16 February 2011 16:06, Felipe Espinoza Castillo <fespinozacast-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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:Have you seen http://railscasts.com/episodes/196-nested-model-form-part-1 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> > 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 > > -- > 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. > >-- 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.
Felipe Espinoza Castillo
2011-Feb-16 17:50 UTC
Re: Solving a problem: keep tracking of an index through controller & view
thanks, that was my problem, I will try to adapt that episode to rails 3, but the essence of the problem was the same. -- 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.