Reputation: 7303
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
)
Upvotes: 12
Views: 3829
Reputation: 111
Stumbled across this post and decided to add a solution that I'm using in Rails 4.1.
class CoolController < ApplicationController
respond_to :html, :js
def controller_action
# controller stuff
respond_to do |format|
format.html {
return redirect_to my_redirect_path, notice: "Successfully updated payment method!"
}
format.js
end
end
end
js file named controller_action.js.erb
is executed and the page is successfully redirected to my_redirect_path
.
Upvotes: 0
Reputation: 12818
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
Upvotes: 2
Reputation: 11069
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.
Upvotes: 5