链接和参数

时间:2014-01-12 10:56:53

标签: ruby-on-rails ruby hash parameters hyperlink

我正在努力获得一系列链接。有些应该可以切换,有些则不可以。

基本上在页面重新加载时,我的params散列已经有两个键,它不应该有,我无法看到它们的添加位置。

我的链接:

<ul class="all filter__group">
  <li><%= link_to "View all", coasters_path(page: 1), remote: true %></li>
</ul>

<ul class="types filter__group">
  <%= content_tag(:li, class: ("active" if params[:type] == "steel")) do %>
    <%= link_to "Steel", coasters_path(type_parameters("steel", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:type] == "wood")) do %>
    <%= link_to "Wood", coasters_path(type_parameters("wood", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:type] == "powered")) do %>
    <%= link_to "Powered", coasters_path(type_parameters("powered", page: 1)), remote: true %>
  <% end %>
</ul>

<ul class="letters filter__group">
  <%= content_tag(:li, class: ("active" if params[:letters] == "#")) do %>
    <%= link_to "#", coasters_path(letter_parameters("#", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:letters] == "a-e")) do %>
    <%= link_to "A-E", coasters_path(letter_parameters("a-e", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:letters] == "f-j")) do %>
    <%= link_to "F-J", coasters_path(letter_parameters("f-j", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:letters] == "k-o")) do %>
    <%= link_to "K-O", coasters_path(letter_parameters("k-o", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:letters] == "p-t")) do %>
    <%= link_to "P-T", coasters_path(letter_parameters("p-t", page: 1)), remote: true %>
  <% end %>
  <%= content_tag(:li, class: ("active" if params[:letters] == "u-z")) do %>
    <%= link_to "U-Z", coasters_path(letter_parameters("u-z", page: 1)), remote: true %>
  <% end %>
</ul>
<%= debug params %>

助手方法:

  # Add the type parameter to URL if not already there and remove if they are
  def type_parameters(type, options = {})
    parameters = {}
    if params[:type] == type
      # Get exisiting params
      parameters = params
      # Remove type
      parameters.delete(:type)
      # Merge any parameters passed in via options
      parameters.merge!(options)
    else
      # Get exisiting params
      parameters = params
      #Add type to new parameters variable
      parameters.merge!(type: type)
      # Merge any parameters passed in via options
      parameters.merge!(options)
    end
    parameters
  end

  # Add the letters parameter to URL if not already there and remove if they are
  def letter_parameters(letters, options = {})
    parameters = {}
    if params[:letters] == letters
      # Get exisiting params
      parameters = params
      # Remove letters
      parameters.delete(:letters)
      # Merge any parameters passed in via options
      parameters.merge!(options)
    else
      # Get exisiting params
      parameters = params
      #Add letters to new parameters variable
      parameters.merge!(letters: letters)
      # Merge any parameters passed in via options
      parameters.merge!(options)
    end
    parameters
  end

在页面重新加载时,这是当前的params哈希:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
action: index
controller: coasters
type: powered
page: 1
letters: u-z

关于我哪里出错的任何想法?

1 个答案:

答案 0 :(得分:0)

您正在帮助者中覆盖params哈希:当您使用merge!时,您将覆盖原始的params哈希值。

这应该有效:

# Add the letters parameter to URL if not already there and remove if they are
  def letter_parameters(letters, options = {})
    parameters = params.dup

    if params[:letters] == letters
      # Remove letters
      parameters.delete(:letters)
    else
      # Add letters to new parameters variable
      parameters.merge!(letters: letters)
    end

    # Merge any parameters passed in via options
    parameters.merge(options)
  end

请注意使用dup创建params散列的新副本