Reputation: 7210
I have 3 lists of links on a page of my site which should change there parameters. The links are:
ul
li = link_to "View all", coasters_path
ul
li
- if params[:type] == "steel"
- params.delete :type
= link_to "Steel", coasters_path(params)
- else
= link_to "Steel", coasters_path(params.merge({type: "steel"}))
li
- if params[:type] == "wood"
- params.delete :type
= link_to "Wooden", coasters_path(params)
- else
= link_to "Wooden", coasters_path(params.merge({type: "wood"}))
li
- if params[:type] == "powered"
-params.delete :type
= link_to "Powered", coasters_path(params)
- else
= link_to "Powered", coasters_path(params.merge({type: "powered"}))
ul
li
- if params[:letters] == "#"
- params.delete :letters
= link_to "#", coasters_path(params)
- else
= link_to "#", coasters_path(params.merge({letters: "#"}))
li
- if params[:letters] == "a-e"
- params.delete :letters
= link_to "A-E", coasters_path(params)
- else
= link_to "A-E", coasters_path(params.merge({letters: "a-e"}))
li
- if params[:letters] == "f-j"
- params.delete :letters
= link_to "F-J", coasters_path(params)
- else
= link_to "F-J", coasters_path(params.merge({letters: "f-j"}))
li
- if params[:letters] == "k-o"
- params.delete :letters
= link_to "K-O", coasters_path(params)
- else
= link_to "K-O", coasters_path(params.merge({letters: "k-o"}))
li
- if params[:letters] == "p-t"
- params.delete :letters
= link_to "P-T", coasters_path(params)
- else
= link_to "P-T", coasters_path(params.merge({letters: "p-t"}))
li
- if params[:letters] == "u-z"
- params.delete :letters
= link_to "U-Z", coasters_path(params)
- else
= link_to "U-Z", coasters_path(params.merge({letters: "u-z"}))
The problem, is if I go to the page afresh at just coasters_path
or /coasters
and click the Steel link, that is fine so the params are ?type=steel. However, I then click on A-E and the params deletes the type=steel and replaces it with letters=a-e.
If I do this the opposite way around and do A-E first followed by Steel, it adds ?letters=a-e and then adjoins &type=steel onto the end thereby chaining the filters.
Any idea why this is happening and how it can be solved? I just cannot see any problems with the code?
Upvotes: 0
Views: 167
Reputation: 84114
The problem is that you are mutating the params hash as you go along: when you are on the steel page then you delete :type
from params pretty much straightaway, so it's not in the hash by the time you are rendering your letters links.
I don't entirely understand what you are trying to do but I would consider mutating the params hash like this really bad form. It's also messy have all that manipulation in the view.
If you need to manipulate the params then write a helper method that returns an updated copy, eg you might write something like
def params_skipping_type type_to_skip
if params[:type] == type_to_skip
params.except(:type)
else
params.merge(:type => type_to_skip)
end
end
Then you can get rid of those if statements in your view and just write
= link_to "Steel", coasters_path(params_skipping_type("steel"))
Upvotes: 1