is there a ways to get a unique id (number) which is unique for each
executing page … sometimes i need a unique number to identify html
elements in order to access them later with javascript.
i would like to call
some hints?
is there a ways to get a unique id (number) which is unique for each
executing page … sometimes i need a unique number to identify html
elements in order to access them later with javascript.
i would like to call
some hints?
You could always use the controller and action name.
First, make a helper method within helpers/application_helper.rb
def page_id
“#{controller.controller_name}_#{controller.action_name}”
end
If you call /projects/new this will contain “projects_new”
That would be pretty unique for a page. I’m sure there are other ways to
do
that though.
Good luck!
On Dec 17, 2007 10:41 AM, Michal G. <
On Dec 17, 2007, at 11:08 AM, Michal G. wrote:
this should produce something like…
You could possibly do something like this:
def unique_id
key = controller_name.to_sym
if !session[key]
session[key] = 0
end
session[key] += 1
return session[key]
end
Peace,
Phillip
thanks brian for the quick response, but what i am looking for is a
unique id (per page) … so whenever i call unique_id it should be a
unique id … this would be just unique per action.
i need e.g.
this should produce something like…
important is that the ids are unique per page so i can call them with
javascript
Sure there is. You just need an instance variable.
def unique_id
@unique_id ||= 0 #use what you have in instance, or set it to 0
@unique_id += 1
end
On Dec 17, 2007 11:36 AM, Michal G. <
thats possible but using a session for such common task is a bit
overkill for me … there must be an easier solution …
By the way, if you’re doing this in a loop, there’s an easier way.
<% @projects.each_with_index do |@project |@project, index| %>
<% end %>
Or use the id of the class
<% @projects.each do |@project |@project| %>
<% end %>
or if you’re using Rails 2.0
<% @projects.each do |@project |@project| %>
<% div_for @project do %>
<% end %>
<% end %>
I guess you need Simply Helpful Plugin. It is now part of Rails 2.0.
Check
out this link:
On Dec 17, 2007 8:41 AM, Michal G. <
On Dec 17, 2007, at 12:43 PM, Brian H. wrote:
Sure there is. You just need an instance variable.
def unique_id
@unique_id ||= 0 #use what you have in instance, or set it to 0
@unique_id += 1
end
Heh, don’t know why I was thinking that the unique_id value would
need to survive page changes. The instance variable is a much better
idea.
Peace,
Phillip
thanks brian … thats exactly what i was looking for …
i tried quite the same but it didnt work …
module ApplicationHelper
@unique_id = 0
def unique_id
@unique_id += 1
end
end
this didnt work because a module cannot have an instance variable …
cause the helpers gets mixed in somehow…
thanks for your help …
Brian H. wrote:
Sure there is. You just need an instance variable.
def unique_id
@unique_id ||= 0 #use what you have in instance, or set it to 0
@unique_id += 1
endOn Dec 17, 2007 11:36 AM, Michal G. <
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs