我的网站页面上有3个链接列表,应该更改参数。链接是:
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"}))
问题是,如果我只是在coasters_path
或/coasters
重新访问页面并单击钢链接,那很好,所以params是?type = steel。然而,我然后点击A-E并且params删除type = steel并用letters = a-e替换它。
如果我以相反的方式执行此操作并首先执行A-E,然后使用Steel,它会添加?letters = a-e然后将end&type; type = steel添加到末尾,从而链接过滤器。
知道为什么会这样,以及如何解决这个问题?我只是看不到代码的任何问题?
答案 0 :(得分:1)
问题在于你正在改变params散列:当你在钢页上时,你会从params中删除:type
几乎是直接的,所以当你是时,它不在哈希中渲染你的字母链接。
我并不完全明白你想要做什么,但我会考虑改变params hash这样的非常糟糕的形式。在视图中有所有操作也很混乱。
如果你需要操作参数,那么编写一个返回更新副本的辅助方法,例如你可能会写一些像
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
然后你可以在你的视图中删除那些if语句并写下
= link_to "Steel", coasters_path(params_skipping_type("steel"))