unique id per page (controller)

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!

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

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

By the way, if you’re doing this in a loop, there’s an easier way.

<% @projects.each_with_index do |@project, index| %>

<% end %>

Or use the id of the class

<% @projects.each do |@project| %>

<% end %>

or if you’re using Rails 2.0

<% @projects.each do |@project| %>

<% div_for @project do %>

<% end %>

<% 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