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

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

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 [email protected]

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

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 [email protected]

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 [email protected]

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