Rails app (3.2.8) and turbolinks (not sure if relevant) enabled.
link
on a users show page. (E.g. A notification that something has 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
)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With