12

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)

4

3 回答 3

5

除了在重定向到的页面上包含 javascript 之外,您无法在重定向后运行 javascript。

您想要的是将此请求中的信息传递到下一个(重定向的)请求。

闪光灯是一个很好的方法来做到这一点。通常你会用它来发送短信:

redirect_to project, notice: "Project foo bar message"

或者

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

没有什么可以阻止您在闪存中使用其他标识符并将 JSON 存储在其中。

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

然后在您的布局中或某处将此 Flash 消息提取到 JavaScript:

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

然后用你的 javascript 魔法来突出显示实际元素。

于 2012-12-19T12:48:20.377 回答
2

一种可能的方式:

在会话中或直接在 cookie 中存储 id(以及可能的对象类型,如果需要突出显示评论)(show伪代码中的操作)

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

在项目控制器show动作

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

在评论视图中

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

像哪里highlight的帮手

def highlight(object, object_id, object_type)
  if object_id and object_type and object.is_a?(object_type.classify.constantize)
    'highlighted'
  end
end 
于 2012-12-19T12:49:00.827 回答
0

偶然发现这篇文章并决定添加一个我在 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 文件controller_action.js.erb被执行,页面成功重定向到my_redirect_path.

于 2021-09-02T20:09:06.303 回答