开发者

Rails: Appending URL parameters & removing URL parameters

开发者 https://www.devze.com 2023-03-24 12:07 出处:网络
EDIT: See below unanswered part II. (ANSWERED) I.Appending URLS I am trying to stack parameters in my view by using basic parameter guidelines:

EDIT: See below unanswered part II.

(ANSWERED) I. Appending URLS

I am trying to stack parameters in my view by using basic parameter guidelines:

users_path(:a => 'test')

The above would return: ?a=test

However, I want to b开发者_JS百科e able to consecutively click:

users_path(:b => 'goat')

and have it return ?a=test&b=goat

Any suggestions on how to stack/append these URL params?

(NOT ANSWERED) II. Removing parameters

If I want to add a [x] link next to a parameter setting, how could I then remove its respective parameter?

(ANSWERED) III. Removing page Parameter

I would like to remove the page parameter when a user selects a parameter choice. Is there a way to do this? It's clear that if I select 'Sports' as a parameter category, pagination shouldn't remain on page, 26, for example.


If you want to append the current parameters, you could try this:

users_path(params.merge(:b => 'goat'))

You may want to write a helper method that does this for you:

def merged_with_current_params(additional)
  params.merge(additional)
end

As to the second part of your question, you probably want to expand the incoming params into a series of checkboxes with the names and values set appropriately. Disabling the checkbox and submitting the form would have the effect of removing that param from the request.

To remove :page parameter, add this to your helper instead:

params.except(:page).merge(additional)


You can use request.query_parameters to use only the params needed, like so:

users_path(request.query_parameters.merge(:b => 'goat'))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号