Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails technique to execute javascript after redirect

Rails app (3.2.8) and turbolinks (not sure if relevant) enabled.

  1. I have some information, a link on a users show page. (E.g. A notification that something has changed.)
  2. When the user clicks on the link I want to direct him to the page and
  3. Visually highlight the elements that changed.

Currently I'm planning to handle it like this:

Create the links so they are of the form:

project2/comment.1453

Create a notifications controller, which gets the projects2 and the type of change comment and its id 1453. So in theory I want to redirect to projects2 and highlight the comment with id 1453 on that page. The problem is: After the redirect how do I highlight the comment?

notificationscontroller.rb (Pseudo code!)

def show
  project = Project.find(params[:project_id])
  comment = Comment.find(params[:commment_id])
  redirect_to project AND highlight!
end

During my research I've come across Backbone, and it looks like Backbones router could solve this problem by responding to the url with a function (the highlighting of the comment). But I don't have any experience with Backbone.

I'm not sure what the general approach to this sort of functionality is. And would like to avoid going down the wrong path. Would be great if you could help me out.

Edit: Sort of a mini question: I'm not sure which character to use for comment.1453 is #a better choice? (comment#1453)

like image 605
wpp Avatar asked Dec 19 '12 10:12

wpp


2 Answers

You can't run javascript after a redirect other than including javascript on the page you redirect to.

What you want is to carry information over from this request to the next (redirected) request.

The flash is a good way to do this. Normally you'ld use it for text messages:

redirect_to project, notice: "Project foo bar message"

or

flash[:notice] = "Project foo bar message"
redirect_to project

There is nothing that stops you from using other identifiers in the flash and storing JSON in their.

flash[:highlight_ids] = "[12, 43, 472, 482]"
redirect_to project

Then in your layout or somewhere extract this flash message to JavaScript:

var highlight_ids = <%= flash[:highlight_ids] %>;

Then do your javascript magic to highlight the actual elements.

like image 151
Ariejan Avatar answered Nov 19 '22 08:11

Ariejan


One of possible ways:

Store the id (and possible object type if need to highlight not only comments) in the session or directly in the cookie (show action in your pseudo-code)

def show
  project = Project.find(params[:project_id])
  comment = Comment.find(params[:commment_id])
  cookies[:highlight_id] = comment.id
  cookies[:highlight_type] = 'Comment' # optionally
  redirect_to project
end

In projects controller show action

def show
  ...
  if cookies[:highlight_id] and cookies[:highlight_type]
    @highlight_id = cookies[:highlight_id]
    @highlight_type = cookies[:highlight_type]
    cookies.delete[:highlight_id]
    cookies.delete[:highlight_type]
  end

And in the comments view

<div class="some_class <%= highlight(@comment, @highlight_id, @highlight_type %>" ...

Where highlight is a helper like

def highlight(object, object_id, object_type)
  if object_id and object_type and object.is_a?(object_type.classify.constantize)
    'highlighted'
  end
end 
like image 42
dimuch Avatar answered Nov 19 '22 09:11

dimuch