admin管理员组文章数量:1287576
Rails app (3.2.8) and turbolinks (not sure if relevant) enabled.
- I have some information, a
link
on a users show page. (E.g. A notification that something has changed.) - When the user clicks on the link I want to direct him to the page and
- 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/ment.1453
Create a notifications controller,
which gets the projects2
and the type of change ment
and its id 1453
. So in theory I want to redirect to projects2
and highlight the ment with id 1453 on that page. The problem is: After the redirect how do I highlight the ment?
notificationscontroller.rb (Pseudo code!)
def show
project = Project.find(params[:project_id])
ment = Comment.find(params[:mment_id])
redirect_to project AND highlight!
end
During my research I've e across Backbone, and it looks like Backbones router could solve this problem by responding to the url with a function (the highlighting of the ment). 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 ment.1453
is #
a better choice? (ment#1453
)
Rails app (3.2.8) and turbolinks (not sure if relevant) enabled.
- I have some information, a
link
on a users show page. (E.g. A notification that something has changed.) - When the user clicks on the link I want to direct him to the page and
- 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/ment.1453
Create a notifications controller,
which gets the projects2
and the type of change ment
and its id 1453
. So in theory I want to redirect to projects2
and highlight the ment with id 1453 on that page. The problem is: After the redirect how do I highlight the ment?
notificationscontroller.rb (Pseudo code!)
def show
project = Project.find(params[:project_id])
ment = Comment.find(params[:mment_id])
redirect_to project AND highlight!
end
During my research I've e across Backbone, and it looks like Backbones router could solve this problem by responding to the url with a function (the highlighting of the ment). 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 ment.1453
is #
a better choice? (ment#1453
)
3 Answers
Reset to default 5You 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 ments) in the session or directly in the cookie (show
action in your pseudo-code)
def show
project = Project.find(params[:project_id])
ment = Comment.find(params[:mment_id])
cookies[:highlight_id] = ment.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 ments view
<div class="some_class <%= highlight(@ment, @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
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
.
本文标签: jqueryRails technique to execute javascript after redirectStack Overflow
版权声明:本文标题:jquery - Rails technique to execute javascript after redirect - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741281093a2370011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论