【问题标题】:How to display and link an certain attribute of an object如何显示和链接对象的某个属性
【发布时间】:2016-12-21 21:03:26
【问题描述】:

我正在尝试显示我的应用程序上正在讨论的所有主题,因此我创建了一个下拉菜单(顺便说一句,如果有更好的方法,请随时分享):

<div class="form-group">
        <label for="topic_category">Category</label>
          <select id="topic_category" name="topic[category]" class="form-control">

            <option>Art</option>
            <option>Business</option>
            <option>Books</option>
            <option>Charity</option>
            <option>Coding</option>
            <option>Cooking</option>
            <option>Dance</option>
            <option>Design</option>
            <option>DIY</option>
            <option>Engineering</option>
            <option>Fashion</option> etc...


          </select>
      </div>

我要做的是创建指向每个单独类别的链接,并通过诸如

之类的方式显示有多少主题处于活动状态

-- 编辑 1 -----

我想要实现的是这样的:

<div class="row">
  <div class="col-md-6" style ="padding-top: 10px; border-right: 1px solid #ccc;">
    <h4 style="text-align: center;">Topics</h4>
      <link_to project.category(Art)> (project.category(art).count)
  </div>

我知道这是错误的,但这是我最接近解释我想要实现的目标

---- 编辑 2 -----

所以我还在努力把它弄好,这可能是因为我是新手。所以按照你的回答,我实现了看起来像这样的代码。

static_pages 控制器。

 def home
    if logged_in?
      @user = current_user
      @topics_count = Topic.group(:category).count
      end
    end

    def categoryselection
      category = params[:category]
      topics = Topic.where(category: category)

      render json: { success: true, Topics: topics }
    end


def help
end

def about
end

def contact
end

end

主页视图

   .......
    <% if logged_in? %>

  <div class="row">
        <%= render 'shared/sidebar_right' %>
      <div class="col-md-8" style = "align-content: right">
        <%= render 'shared/feed' %>
      </div>

  </div>

<% else %>
 .....

Sidebar_right 视图

            ......
            <div id='topics'>
            <% @topics_count.each do |topic, count| %>
            <a class ='project-link' href='topic_path?category=<%= topic %>'> <%= topic %> (<%= count %>)</a>
            <% end %>
            ......

<script type>

$('#topics').on('change', '.topic-link' function (e) {
  e.preventDefault();
  var category = $(e.currentTarget).val();
  var queryUrl = $(e).href + '?category=' + category;
  $.get(queryUrl, function(resp) {
    if (resp.success) {redirect_to topic_path 
      // select the div or whatever node on the DOM you are displaying the result, and change it.
    }
  });
});
</script>

主题控制器

class TopicsController < ApplicationController

.....

def index
end

def show
  @user = current_user
    @topic = Topic.find(params[:id])

end

.....

我得到的错误是没有路由匹配 [GET] topic_path,我检查了我的路由并退出它实际上是指“显示”,这是因为我使用的页面是主页而不是主题页面?

谢谢!

【问题讨论】:

  • 您的意思是链接 onclick/onchange 事件吗? HTML 选择元素不能包含链接。
  • @Fallenhero 感谢您的回答,所以我的意思是列出所有可能的主题类别,以及指向这些主题的链接(取决于它们的类别)。这将显示在我的主页上,因此人们可以单击它们,并且只能看到他们单击的那些主题类别。
  • 只需将其作为查询参数传入并创建一个where 查询即可解决问题

标签: javascript css ruby-on-rails


【解决方案1】:

基于您的新编辑

在控制器中:

def index
  @topics_count = Topic.group(:category).count
  # this will give you something like this { 'Art' => 10, 'Business' => 15' }
end

在html页面上:

<div id='topics'>
  <% @topics_count.each do |topic, count| %>
    <a class='topic-link' href='topic_show_url?category=<%= topic %>'> <%= topic %> (<%= count %>)</a>
  <% end %>
</div>

您不需要(也不应该)加载所有主题,因为您只是显示计数,而且在大规模情况下,加载整个表格可能会使您的网站崩溃。

这将处理您的第二个请求,显示用户点击链接时选择的主题。

在页面上,当用户选择其中一个选项时发送 ajax 请求。

$('#topics').on('click', '.topic-link', function (e) {
  e.preventDefault();
  var category = $(e.currentTarget).val();
  var queryUrl = $(e).href + '?category=' + category;
  $.get(queryUrl, function(resp) {
    if (resp.success) {
      // select the div or whatever node on the DOM you are displaying the result, and change it.
    }
  });
});

在控制器中

def you_define_this_method
  category = params[:category]
  topics = Topic.where(category: category)

  render json: { success: true, topics: topics }
end

【讨论】:

  • 感谢您的评论,我添加了一个编辑 1,您提出的代码是否类似?
  • @Eltorero1992 我的回答将允许您通过更新当前页面来显示不同类别的主题,而无需将用户重定向到不同的页面。类似于大多数电子商务网站中的过滤器的作用。然后构造到主题的链接只是简单地从控制器返回 url,或者只使用 id,并在 javascript 中构造它。
  • @Eltorero1992 添加了可以满足您需求的编辑。
  • 正是我需要的谢谢!但现在我遇到了另一个问题,因为我需要在主题控制器中修改 show 以便只显示那些项目,有什么想法吗?
  • @Eltorero1992 刚刚回滚了我的答案。第二部分应该这样做。
【解决方案2】:

您可以在&lt;option&gt; 标签内添加一个链接...即。

    <option><a href="www.something.com">Art</a></option>

【讨论】:

  • 我想要达到的效果就像我在编辑 1 中展示的那样
猜你喜欢
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多