【问题标题】:Ruby On Rails - Acts As Taggable - Change name of param: keywordRuby On Rails - 充当可标记 - 更改参数名称:关键字
【发布时间】:2012-05-29 17:28:49
【问题描述】:

我想知道当使用作为可标记时是否/如何更改参数名称:keyword?

今天我的网址是这样的:

http://www.foo.bar/tagged?keyword=baz

我想把“关键字”换成另一个词。

控制器

  def tagged
    @current_keyword = params[:keyword]
    @tags = FeedEntry.tag_counts_on(:keyword)
    @tagged_feed_entries = FeedEntry.tagged_with(params[:keyword]).order("published_at desc").paginate :page => params[:sida]
  end

查看:

 <table class="table table-striped">
    <tbody>
    <% if @tags %>

        <% @tagged_feed_entries.each do |feed_entry| %>
            <tr>
              <td>
                <% today = Date.today %>
                <% if (today === feed_entry.published_at.to_date) %>
                    <span class="label label-success">
                <% else %>
                    <span class="label">
                <% end %>
                <%= format_stamp_to_date(feed_entry.published_at) %>
                kl:
                <%= I18n.localize(feed_entry.published_at, :format => '%H:%M') %>
                </span>


              </td>
              <td><%= link_to feed_entry.name, feed_entry_path(feed_entry) %></td>
            </tr>
        <% end %>

    <% end %>
    </tbody>
  </table>

  <%= will_paginate @tagged_feed_entries, :param_name => :sida %>

【问题讨论】:

  • 你的控制器/视图代码是什么样的?
  • @xnm 我已经更新了我的问题,谢谢! :)

标签: ruby-on-rails keyword acts-as-taggable-on param


【解决方案1】:

您应该能够将params[:keyword] 的所有实例替换为params[:whatever]。然后你的路径变成http://www.foo.bar/tagged?whatever=baz

如果您有一个搜索表单,您也必须在那里进行相关更改。

【讨论】:

  • 这样做时我面临的问题是我以前的所有“关键字”都消失了......如何将旧关键字与新参数匹配?
  • 或者我是否需要将旧关键字转移/重命名为数据库中我的参数名称的新行?
  • 它根本不会影响您的记录,除非您也更改了FeedEntry.tag_counts_on(:keyword)
  • 另外,虽然没有必要重命名您的列,但这可能是最佳实践,可以让您执行质量分配(例如FeedEntry.create(params[:feed_entry])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多