conditionally contain arguments in url path in rails
up vote
3
down vote
favorite
I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " "
then the redirect_to only includes the first argument.
How would I best achieve this?
Thanks in advance!
ruby-on-rails ruby
add a comment |
up vote
3
down vote
favorite
I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " "
then the redirect_to only includes the first argument.
How would I best achieve this?
Thanks in advance!
ruby-on-rails ruby
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " "
then the redirect_to only includes the first argument.
How would I best achieve this?
Thanks in advance!
ruby-on-rails ruby
I have a search function which allows a user to search for items. From the generated list, the user can add items to a list. After adding an item from the list, the page reloads and the user is returned to their current search list. However, currently, if a user has not searched for anything, and just adds an item of the top of the list (starting at 'A, for example), when the page reloads it includes a blank search query in the url. I know why, it is because of this line
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
I want to make the last argument in the redirect path a conditional one so that, if say the :search_term == nil || == "" || " "
then the redirect_to only includes the first argument.
How would I best achieve this?
Thanks in advance!
ruby-on-rails ruby
ruby-on-rails ruby
asked Jul 21 '14 at 19:57
rico_mac
478314
478314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can do:
if params[:search_term].present?
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
else
redirect_to admin_job_job_products_path(@job)
end
Or
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)
References:
.present?
method documentation
.blank?
method documentation
The 2nd example doesn't work properly. Please update your answer to match the other user's answerredirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
add a comment |
up vote
1
down vote
The second example of MrYoshiji probably will not work, but you can write it short and simple this why:
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
So if the result will be nil
rails will not include that param in the url
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can do:
if params[:search_term].present?
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
else
redirect_to admin_job_job_products_path(@job)
end
Or
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)
References:
.present?
method documentation
.blank?
method documentation
The 2nd example doesn't work properly. Please update your answer to match the other user's answerredirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
add a comment |
up vote
2
down vote
accepted
You can do:
if params[:search_term].present?
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
else
redirect_to admin_job_job_products_path(@job)
end
Or
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)
References:
.present?
method documentation
.blank?
method documentation
The 2nd example doesn't work properly. Please update your answer to match the other user's answerredirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can do:
if params[:search_term].present?
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
else
redirect_to admin_job_job_products_path(@job)
end
Or
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)
References:
.present?
method documentation
.blank?
method documentation
You can do:
if params[:search_term].present?
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term])
else
redirect_to admin_job_job_products_path(@job)
end
Or
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].presence)
References:
.present?
method documentation
.blank?
method documentation
edited 19 hours ago
answered Jul 21 '14 at 20:01
MrYoshiji
44.1k89391
44.1k89391
The 2nd example doesn't work properly. Please update your answer to match the other user's answerredirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
add a comment |
The 2nd example doesn't work properly. Please update your answer to match the other user's answerredirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
The 2nd example doesn't work properly. Please update your answer to match the other user's answer
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
The 2nd example doesn't work properly. Please update your answer to match the other user's answer
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
– Jeremy Moritz
yesterday
add a comment |
up vote
1
down vote
The second example of MrYoshiji probably will not work, but you can write it short and simple this why:
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
So if the result will be nil
rails will not include that param in the url
add a comment |
up vote
1
down vote
The second example of MrYoshiji probably will not work, but you can write it short and simple this why:
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
So if the result will be nil
rails will not include that param in the url
add a comment |
up vote
1
down vote
up vote
1
down vote
The second example of MrYoshiji probably will not work, but you can write it short and simple this why:
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
So if the result will be nil
rails will not include that param in the url
The second example of MrYoshiji probably will not work, but you can write it short and simple this why:
redirect_to admin_job_job_products_path(@job, search_term: params[:search_term].present? ? params[:search_term] : nil)
So if the result will be nil
rails will not include that param in the url
answered Jun 4 '17 at 20:21
GEkk
188312
188312
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24873604%2fconditionally-contain-arguments-in-url-path-in-rails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown