使用 :scope 显示结果而不添加新操作

发布于 2024-12-12 01:37:39 字数 303 浏览 0 评论 0原文

我希望在我的 Rails 3.1 应用程序中使用 :scope ,并且需要一些关于如何正确实现它们的指导。我知道您在模型中编写逻辑(以提取您需要的数据),但我的问题来自后续步骤。

例如:我有一条“Piece”信息。每个“作品”都有一个主题和受众。我想使用范围来显示给定主题或受众的所有“片段”信息,而无需在控制器中编写新操作。

本质上,我不确定如何链接到这些视图,而不在控制器中创建新操作,然后仅使用典型的 link_to。我想使用我编写的范围。

我正在寻找正确的(也是最合适的)方法来完成这项工作。

预先感谢您对此的任何帮助。

I'm looking to use :scope in my rails 3.1 app and need a little direction on how to implement them correctly. I understand that you write the logic in the model (to extract the data you need), but my question comes on the next steps.

For example: I have a "Piece" of information. Each "Piece" has a Topic and Audience among other things. I'd like to use scopes to display all "Pieces" of information for a given Topic or Audience without writing new actions in my controller.

Essentially, I'm not sure how to link to these views without creating new actions in the controller and then just using the typical link_to. I'd like to use the scopes I've written.

I'm looking for the correct (and most appropriate) way to get this done.

Thanks in advance for any help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-12-19 01:37:39

您甚至不需要范围。看看:

Piece.includes([:topic, :audience]).where(['`topics`.name = ?', 'Politics'])

使用 Rails 3 &阿雷尔,大多数瞄准镜都是完全没有必要的。

在视图中使用此功能而不创建新的控制器操作时,您可以使用传递给索引操作的参数设置一些条件,以确定如何满足请求。


例如:(这是一个粗糙未经测试的示例,但它应该可以工作)

app/controllers/pieces_controller.rb:

class PieceController < ApplicationController

  def index
    case params[:find_by]
      when 'topic_name'
        @pieces = Piece.includes([:topic, :audience]).where(['`topics`.name = ?', params[:topic_name])
      when 'topic_id'
        @pieces = Piece.includes([:topic, :audience]).where(['`topics`.id = ?', params[:topic_id])
      else
        @pieces = Piece.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @addressees }
    end
  end

  # ...
end

app/views/pieces/index.html.erb:

<%= link_to 'Politics Pieces', :controller => 'pieces', :find_by => 'topic_name', :topic_name => 'Politics' %>

使用选择框和 javascript 的另一个示例:

app/views/pieces/index.html.erb: >

<%= select 'find_by', 'topic', find_by_topic_select_options %>

app/helpers/pieces_helper.rb:(为了便于阅读,将 link_to 移至此处)

module PieceHelper
  def find_by_topic_select_options
    Topic.all.collect do |topic|
      url = url_for :controller => 'pieces', :find_by => 'topic_id', :topic_id => topic.id
      [topic.name, url]
    end
  end
end

public/javascripts/application.js:

window.onload = (function (ev) {
  var find_by_topic_select = document.getElementById('find_by_topic');
  find_by_topic_select.onchange = (function (e) {
    var selected = this.options[this.selectedIndex];
    window.location = selected.value;
  });
});

而且,脚注,您不link_to< /code> 关联或数据结构。您链接到一个 url 或路由,该 url 或路由调用一个操作(呈现一个视图),该操作会适当地显示您的数据结构。我希望这能为您解决问题。

You don't even need a scope for this. Check it out:

Piece.includes([:topic, :audience]).where(['`topics`.name = ?', 'Politics'])

With Rails 3 & Arel, most scopes are entirely unnecessary.

On using this in views without creating new controller actions, you can setup a some conditionals using params passed to your index action to determine how to accomodate the request.


For example: (and this is a rough, untested example, but it should work)

app/controllers/pieces_controller.rb:

class PieceController < ApplicationController

  def index
    case params[:find_by]
      when 'topic_name'
        @pieces = Piece.includes([:topic, :audience]).where(['`topics`.name = ?', params[:topic_name])
      when 'topic_id'
        @pieces = Piece.includes([:topic, :audience]).where(['`topics`.id = ?', params[:topic_id])
      else
        @pieces = Piece.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @addressees }
    end
  end

  # ...
end

app/views/pieces/index.html.erb:

<%= link_to 'Politics Pieces', :controller => 'pieces', :find_by => 'topic_name', :topic_name => 'Politics' %>

Another example using a select box and javascript:

app/views/pieces/index.html.erb:

<%= select 'find_by', 'topic', find_by_topic_select_options %>

app/helpers/pieces_helper.rb: (moved link_to here for readibility)

module PieceHelper
  def find_by_topic_select_options
    Topic.all.collect do |topic|
      url = url_for :controller => 'pieces', :find_by => 'topic_id', :topic_id => topic.id
      [topic.name, url]
    end
  end
end

public/javascripts/application.js:

window.onload = (function (ev) {
  var find_by_topic_select = document.getElementById('find_by_topic');
  find_by_topic_select.onchange = (function (e) {
    var selected = this.options[this.selectedIndex];
    window.location = selected.value;
  });
});

And, a footnote, you do not link_to an association or a data structure. You link to a url or route which calls an action (which renders a view) which appropriately displays your data structure. I hope this clears things up for you.

无法言说的痛 2024-12-19 01:37:39

以下是我的做法:

piece.rb

def self.scope_by_topic(topic_name)
  topic_name.present? ? includes(:topic).where(topic: {name: topic_name}) : self.scoped
end

def self.scope_by_audience(audience_name)
  topic_name.present? ? includes(:audience).where(audience: {name: audience_name}) : self.scoped
end

关于我在此使用的 scoped 方法:
http://api.rubyonrails.org/classes/ActiveRecord /NamedScope/ClassMethods.html#method-i-scoped

在您的pieces_controller.rb中:

def index
  @pieces = Piece.scope_by_topic(params[:topic_scope])
                 .scope_by_audience(params[:audience_scope]).all
  # render view here
end

并且在您看来,您可以使用以下内容helper:

link_to "Mypiece of information",pieces_path(topic_scope: 'A topic', Audience_scope: 'An Audience')

URL 将如下所示:

http://www.example。 com/pieces?topic_scope=A+topic&aucience_scope=An+audience

Here is how I would do that:

In piece.rb

def self.scope_by_topic(topic_name)
  topic_name.present? ? includes(:topic).where(topic: {name: topic_name}) : self.scoped
end

def self.scope_by_audience(audience_name)
  topic_name.present? ? includes(:audience).where(audience: {name: audience_name}) : self.scoped
end

About the scoped method I use hereover:
http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html#method-i-scoped

In you pieces_controller.rb:

def index
  @pieces = Piece.scope_by_topic(params[:topic_scope])
                 .scope_by_audience(params[:audience_scope]).all
  # render view here
end

And in your view, you can use the following helper:

link_to "My piece of information", pieces_path(topic_scope: 'A topic', audience_scope: 'An audience')

The URL will look like:

http://www.example.com/pieces?topic_scope=A+topic&aucience_scope=An+audience

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文