Mike Dershowitz
2007-Mar-11 15:40 UTC
RJS & increment variable - this is an interesting one
Hi: I have a small piece of code I''m injecting into a page using a partial and RJS. Trick is, the code I''m injecting is a form field, and the id needs to have a digit at the end of it so I can grab it when I submit the form. I can''t seem to get the partial to render with the variable incremented. My RJS: page.insert_html :bottom, ''new_task'', :partial => ''add_new'' page.visual_effect :highlight, ''new_task'' My partial: NOTE: here''s where it gets interesting - see the first line? I''m trying here to increment the instance variable @task. Note that this @task is set to 3 when the page itself (page = multiple) loads. <% @task = @task.succ %> <tr class="spacerH10"> <td colspan=3></td> </tr> <tr> <td width="38%"> <b>Task <%= @task %>:<b><br /> </td> <td width="62%"> <%= text_field_tag("task#{@task}", nil, {}) %><br /> </td> </tr> Controller method for the page (simple): def multiple @task = 3 end Controller method for the partial taks ("add_new") NOTE: I''ve currently tried it as empty. When I have it as empty the variable never increments in the partial. When I have only one line in this method (@task = @task.succ), I get an error: "NoMethodError in Goals#add_new"; When just set the instance variable (@task = 4), it comes through no problem, but it still doesn''t increment when the partial is rendered. def add_new end The key here is that this partial should be able to be injected into the form infintely, which is essentially what I want, but I can''t get it to increment correctly. Thanks in advance for any help you can provide. Mike -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I use the same idea for enabling users to add infinite number of attachments and other things. However, I don''t use a variable and increment, instead I use the time as follows: partial: <% index = Time.now.tv_sec %> <input id="attachment_data" name="attachment[<%=index%>][data]" size="30" type="file" /> On Mar 11, 6:40 pm, Mike Dershowitz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi: > > I have a small piece of code I''m injecting into a page using a partial > and RJS. Trick is, the code I''m injecting is a form field, and the id > needs to have a digit at the end of it so I can grab it when I submit > the form. I can''t seem to get the partial to render with the variable > incremented. > > My RJS: > > page.insert_html :bottom, ''new_task'', :partial => ''add_new'' > page.visual_effect :highlight, ''new_task'' > > My partial: > > NOTE: here''s where it gets interesting - see the first line? I''m trying > here to increment the instance variable @task. Note that this @task is > set to 3 when the page itself (page = multiple) loads. > > <% @task = @task.succ %> > <tr class="spacerH10"> > <td colspan=3></td> > </tr> > > <tr> > <td width="38%"> > <b>Task <%= @task %>:<b><br /> > </td> > <td width="62%"> > <%= text_field_tag("task#{@task}", nil, {}) %><br /> > </td> > </tr> > > Controller method for the page (simple): > > def multiple > @task = 3 > end > > Controller method for the partial taks ("add_new") > > NOTE: I''ve currently tried it as empty. When I have it as empty the > variable never increments in the partial. When I have only one line in > this method (@task = @task.succ), I get an error: "NoMethodError in > Goals#add_new"; When just set the instance variable (@task = 4), it > comes through no problem, but it still doesn''t increment when the > partial is rendered. > > def add_new > > end > > The key here is that this partial should be able to be injected into the > form infintely, which is essentially what I want, but I can''t get it to > increment correctly. > > Thanks in advance for any help you can provide. > > Mike > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jatinder Singh
2007-Mar-21 09:59 UTC
Re: RJS & increment variable - this is an interesting one
Hi Mike, when the action "add_new" of controller "Goals" is called, a new instance of Goals controller is created and hence previous value of @task is no more accessible in add_new. Hence when you have something like this "@task = @task.succ " in your add_new method, it will give error, since @task is not defined and you are trying to invoke nil.succ. You need to persist @task across requests, 1 way is to use sessions. Another approach could be the one mentioned by Ahmed. Regards Jatinder On 3/12/07, Ahmad <gsunnah-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I use the same idea for enabling users to add infinite number of > attachments and other things. However, I don''t use a variable and > increment, instead I use the time as follows: > > partial: > <% index = Time.now.tv_sec %> > <input id="attachment_data" name="attachment[<%=index%>][data]" > size="30" type="file" /> > > > On Mar 11, 6:40 pm, Mike Dershowitz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > Hi: > > > > I have a small piece of code I''m injecting into a page using a partial > > and RJS. Trick is, the code I''m injecting is a form field, and the id > > needs to have a digit at the end of it so I can grab it when I submit > > the form. I can''t seem to get the partial to render with the variable > > incremented. > > > > My RJS: > > > > page.insert_html :bottom, ''new_task'', :partial => ''add_new'' > > page.visual_effect :highlight, ''new_task'' > > > > My partial: > > > > NOTE: here''s where it gets interesting - see the first line? I''m trying > > here to increment the instance variable @task. Note that this @task is > > set to 3 when the page itself (page = multiple) loads. > > > > <% @task = @task.succ %> > > <tr class="spacerH10"> > > <td colspan=3></td> > > </tr> > > > > <tr> > > <td width="38%"> > > <b>Task <%= @task %>:<b><br /> > > </td> > > <td width="62%"> > > <%= text_field_tag("task#{@task}", nil, {}) %><br /> > > </td> > > </tr> > > > > Controller method for the page (simple): > > > > def multiple > > @task = 3 > > end > > > > Controller method for the partial taks ("add_new") > > > > NOTE: I''ve currently tried it as empty. When I have it as empty the > > variable never increments in the partial. When I have only one line in > > this method (@task = @task.succ), I get an error: "NoMethodError in > > Goals#add_new"; When just set the instance variable (@task = 4), it > > comes through no problem, but it still doesn''t increment when the > > partial is rendered. > > > > def add_new > > > > end > > > > The key here is that this partial should be able to be injected into the > > form infintely, which is essentially what I want, but I can''t get it to > > increment correctly. > > > > Thanks in advance for any help you can provide. > > > > Mike > > > > -- > > Posted viahttp://www.ruby-forum.com/. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---