# Assign counter to view......is it possible?

**URL:** <https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676>\
**Category:** Rails\
**Created:** [January 11, 2008, 3:09am UTC](https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676 "2008-01-11T03:09:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 3:09am UTC](https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676/1 "2008-01-11T03:09:08Z")

</div>

Hi,  
I really want to know if it is possible to assign counter to the .rhtml  
page so that if counter=1, when the submit button is clicked, I will do  
the save/create action. If counter \>= 2, when the submit button is  
clicked, I will do the update action.

Thanks,  
user splash

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 13, 2008, 10:53am UTC](https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676/2 "2008-01-13T10:53:45Z")

</div>

Neon wrote:

> You could accomplish this with the use of sessions. I’m really still  
> new to RoR. But basically with sessions enabled you can add code to  
> your page controller like:
> 
> def find\_count  
> session[:count] ||= 0 // Looks to see if session[:count] has been  
> initialized, if not it will in this case set it to a value of 0.  
> session[:count += 1 // increment by 1. First visit will be  
> initialized to 1. Successive visits will increment as stated earlier,  
> by 1.  
> end
> 
> then in the view code do something a control structure check like
> 
> \<% if session[:count] == 1 %\>  
> \<% form\_for :model, :url =\> { :action =\> :save } do |form| %\>  
> \<%= render :partial =\> ‘form’ %\>  
> \<%= submit\_tag “Save” %\>  
> \<% end %\>  
> \<% elsif session[:count] \> 1 %\>  
> \<% form\_for :model, :url =\> { :action =\> :edit } do |form| %\>  
> \<%= render :partial =\> ‘form’ %\>  
> \<%= submit\_tag “Edit” %\>  
> \<% end %\>  
> \<% end %\>
> 
> Where the above render code will look for a file called \_form.rhtml in  
> the same folder and render the contents of that file, in this case it  
> should contain the fields for your model.  
> On Jan 10, 6:09 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi,  
The above method allows me to save the data when i first time i enter  
the page allows me to edit the subsequent time i enter that page. But  
what should i do to enable the user to update that data that was saved  
previously ? I thought of using the find current id to edit the data.  
But it will not be practical as the current id of the data might not be  
that particular i want to edit.Can someone tell me what should i do?

Thanks,  
user splash

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 11, 2008, 11:08am UTC](https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676/3 "2008-01-11T11:08:54Z")

</div>

You could accomplish this with the use of sessions. I’m really still  
new to RoR. But basically with sessions enabled you can add code to  
your page controller like:

def find\_count  
session[:count] ||= 0 // Looks to see if session[:count] has been  
initialized, if not it will in this case set it to a value of 0.  
session[:count += 1 // increment by 1. First visit will be  
initialized to 1. Successive visits will increment as stated earlier,  
by 1.  
end

then in the view code do something a control structure check like

\<% if session[:count] == 1 %\>  
\<% form\_for :model, :url =\> { :action =\> :save } do |form| %\>  
\<%= render :partial =\> ‘form’ %\>  
\<%= submit\_tag “Save” %\>  
\<% end %\>  
\<% elsif session[:count] \> 1 %\>  
\<% form\_for :model, :url =\> { :action =\> :edit } do |form| %\>  
\<%= render :partial =\> ‘form’ %\>  
\<%= submit\_tag “Edit” %\>  
\<% end %\>  
\<% end %\>

Where the above render code will look for a file called \_form.rhtml in  
the same folder and render the contents of that file, in this case it  
should contain the fields for your model.  
On Jan 10, 6:09 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fries88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fries88](https://www.ruby-forum.com/u/fries88)\
**Post date:** [January 13, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/assign-counter-to-view-is-it-possible/125676/4 "2008-01-13T16:02:15Z")

</div>

fries 88 wrote:

> Neon wrote:
> 
> > You could accomplish this with the use of sessions. I’m really still  
> > new to RoR. But basically with sessions enabled you can add code to  
> > your page controller like:
> > 
> > def find\_count  
> > session[:count] ||= 0 // Looks to see if session[:count] has been  
> > initialized, if not it will in this case set it to a value of 0.  
> > session[:count += 1 // increment by 1. First visit will be  
> > initialized to 1. Successive visits will increment as stated earlier,  
> > by 1.  
> > end
> > 
> > then in the view code do something a control structure check like
> > 
> > \<% if session[:count] == 1 %\>  
> > \<% form\_for :model, :url =\> { :action =\> :save } do |form| %\>  
> > \<%= render :partial =\> ‘form’ %\>  
> > \<%= submit\_tag “Save” %\>  
> > \<% end %\>  
> > \<% elsif session[:count] \> 1 %\>  
> > \<% form\_for :model, :url =\> { :action =\> :edit } do |form| %\>  
> > \<%= render :partial =\> ‘form’ %\>  
> > \<%= submit\_tag “Edit” %\>  
> > \<% end %\>  
> > \<% end %\>
> > 
> > Where the above render code will look for a file called \_form.rhtml in  
> > the same folder and render the contents of that file, in this case it  
> > should contain the fields for your model.  
> > On Jan 10, 6:09 pm, user splash [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> Hi,  
> The above method allows me to save the data when i first time i enter  
> the page allows me to edit the subsequent time i enter that page. But  
> what should i do to enable the user to update that data that was saved  
> previously ? I thought of using the find current id to edit the data.  
> But it will not be practical as the current id of the data might not be  
> that particular i want to edit.Can someone tell me what should i do?
> 
> Thanks,  
> user splash

Hi,  
Adding on to the previous posting’s question, does anyone also know how  
to end the session? Like when ever I enter the main page a new session  
is created and when i click a button/link, the session will be ended.

Thanks  
fries 88
