How to get the checked boxes with link_to into params?
I have a Rails 5.2.1 app with Movies and Categories. They are connected to one another through a has_and_belongs_to_many
relation with a join table.
Trying to do the following: on the index
page for Movies, I want to filter the collection of movies that is shown by checking Category check boxes. I can properly show check boxes for the Categories, but I'm having a hard time getting the information about what check boxes are checked into the params.
/ rails_app/app/views/movies/index.html.slim
h1 Listing movies
= collection_check_boxes(@movies, :category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= link_to 'Filter movies on category', params.permit!.merge(filter: :category_ids)
table
/ table with movies that will be filtered
These :category_ids
seem to be wrong. Is it possible to somehow get at the check box results in this way (for further filtering with query string parameters)? Am I missing something, e.g. in my controller?
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
...
def movie_params
params.require(:movie).permit(:name, :rating, category_ids: )
end
end
The above is an example app, generated with some scaffolds and a few edits:
rails generate scaffold category name:string
rails generate scaffold movie name:string rating:integer
rails generate migration CreateJoinTableMoviesCategories movie category
bin/rails db:migrate RAILS_ENV=development
-> add has_and_belongs_to_many :movies
to Category class
-> add has_and_belongs_to_many :categories
to Movie class
-> add category_ids:
to movie_params
in Movie class
ruby-on-rails slim-lang
add a comment |
I have a Rails 5.2.1 app with Movies and Categories. They are connected to one another through a has_and_belongs_to_many
relation with a join table.
Trying to do the following: on the index
page for Movies, I want to filter the collection of movies that is shown by checking Category check boxes. I can properly show check boxes for the Categories, but I'm having a hard time getting the information about what check boxes are checked into the params.
/ rails_app/app/views/movies/index.html.slim
h1 Listing movies
= collection_check_boxes(@movies, :category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= link_to 'Filter movies on category', params.permit!.merge(filter: :category_ids)
table
/ table with movies that will be filtered
These :category_ids
seem to be wrong. Is it possible to somehow get at the check box results in this way (for further filtering with query string parameters)? Am I missing something, e.g. in my controller?
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
...
def movie_params
params.require(:movie).permit(:name, :rating, category_ids: )
end
end
The above is an example app, generated with some scaffolds and a few edits:
rails generate scaffold category name:string
rails generate scaffold movie name:string rating:integer
rails generate migration CreateJoinTableMoviesCategories movie category
bin/rails db:migrate RAILS_ENV=development
-> add has_and_belongs_to_many :movies
to Category class
-> add has_and_belongs_to_many :categories
to Movie class
-> add category_ids:
to movie_params
in Movie class
ruby-on-rails slim-lang
Tryparams.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52
add a comment |
I have a Rails 5.2.1 app with Movies and Categories. They are connected to one another through a has_and_belongs_to_many
relation with a join table.
Trying to do the following: on the index
page for Movies, I want to filter the collection of movies that is shown by checking Category check boxes. I can properly show check boxes for the Categories, but I'm having a hard time getting the information about what check boxes are checked into the params.
/ rails_app/app/views/movies/index.html.slim
h1 Listing movies
= collection_check_boxes(@movies, :category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= link_to 'Filter movies on category', params.permit!.merge(filter: :category_ids)
table
/ table with movies that will be filtered
These :category_ids
seem to be wrong. Is it possible to somehow get at the check box results in this way (for further filtering with query string parameters)? Am I missing something, e.g. in my controller?
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
...
def movie_params
params.require(:movie).permit(:name, :rating, category_ids: )
end
end
The above is an example app, generated with some scaffolds and a few edits:
rails generate scaffold category name:string
rails generate scaffold movie name:string rating:integer
rails generate migration CreateJoinTableMoviesCategories movie category
bin/rails db:migrate RAILS_ENV=development
-> add has_and_belongs_to_many :movies
to Category class
-> add has_and_belongs_to_many :categories
to Movie class
-> add category_ids:
to movie_params
in Movie class
ruby-on-rails slim-lang
I have a Rails 5.2.1 app with Movies and Categories. They are connected to one another through a has_and_belongs_to_many
relation with a join table.
Trying to do the following: on the index
page for Movies, I want to filter the collection of movies that is shown by checking Category check boxes. I can properly show check boxes for the Categories, but I'm having a hard time getting the information about what check boxes are checked into the params.
/ rails_app/app/views/movies/index.html.slim
h1 Listing movies
= collection_check_boxes(@movies, :category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= link_to 'Filter movies on category', params.permit!.merge(filter: :category_ids)
table
/ table with movies that will be filtered
These :category_ids
seem to be wrong. Is it possible to somehow get at the check box results in this way (for further filtering with query string parameters)? Am I missing something, e.g. in my controller?
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = Movie.all
end
...
def movie_params
params.require(:movie).permit(:name, :rating, category_ids: )
end
end
The above is an example app, generated with some scaffolds and a few edits:
rails generate scaffold category name:string
rails generate scaffold movie name:string rating:integer
rails generate migration CreateJoinTableMoviesCategories movie category
bin/rails db:migrate RAILS_ENV=development
-> add has_and_belongs_to_many :movies
to Category class
-> add has_and_belongs_to_many :categories
to Movie class
-> add category_ids:
to movie_params
in Movie class
ruby-on-rails slim-lang
ruby-on-rails slim-lang
asked Nov 27 '18 at 21:50
JosienJosien
6,68653044
6,68653044
Tryparams.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52
add a comment |
Tryparams.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52
Try
params.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52
Try
params.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52
add a comment |
1 Answer
1
active
oldest
votes
try this and see if it works for you.
View:
/ rails_app/app/views/movies/index.html.slim
= form_for :movie do |f|
= f.collection_check_boxes(:category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= f.submit
/ table with movies that will be filtered
Controller:
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = if params[:movie]
Movie.joins(:categories).where(categories: { id: params[:movie][:category_ids] })
else
Movie.all
end
end
...
Essentially, wrapping the checkboxes inside a form, then tweak index
action when filter params exists.
Note: I'm not familiar with slim syntax, so tweak it if you get syntax error :).
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without theform_for
, but will probably end up using it anyway.)
– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53508717%2fhow-to-get-the-checked-boxes-with-link-to-into-params%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
try this and see if it works for you.
View:
/ rails_app/app/views/movies/index.html.slim
= form_for :movie do |f|
= f.collection_check_boxes(:category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= f.submit
/ table with movies that will be filtered
Controller:
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = if params[:movie]
Movie.joins(:categories).where(categories: { id: params[:movie][:category_ids] })
else
Movie.all
end
end
...
Essentially, wrapping the checkboxes inside a form, then tweak index
action when filter params exists.
Note: I'm not familiar with slim syntax, so tweak it if you get syntax error :).
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without theform_for
, but will probably end up using it anyway.)
– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
add a comment |
try this and see if it works for you.
View:
/ rails_app/app/views/movies/index.html.slim
= form_for :movie do |f|
= f.collection_check_boxes(:category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= f.submit
/ table with movies that will be filtered
Controller:
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = if params[:movie]
Movie.joins(:categories).where(categories: { id: params[:movie][:category_ids] })
else
Movie.all
end
end
...
Essentially, wrapping the checkboxes inside a form, then tweak index
action when filter params exists.
Note: I'm not familiar with slim syntax, so tweak it if you get syntax error :).
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without theform_for
, but will probably end up using it anyway.)
– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
add a comment |
try this and see if it works for you.
View:
/ rails_app/app/views/movies/index.html.slim
= form_for :movie do |f|
= f.collection_check_boxes(:category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= f.submit
/ table with movies that will be filtered
Controller:
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = if params[:movie]
Movie.joins(:categories).where(categories: { id: params[:movie][:category_ids] })
else
Movie.all
end
end
...
Essentially, wrapping the checkboxes inside a form, then tweak index
action when filter params exists.
Note: I'm not familiar with slim syntax, so tweak it if you get syntax error :).
try this and see if it works for you.
View:
/ rails_app/app/views/movies/index.html.slim
= form_for :movie do |f|
= f.collection_check_boxes(:category_ids, Category.all, :id, :name) do |box|
= box.check_box
= box.label
= f.submit
/ table with movies that will be filtered
Controller:
# rails_app/app/controllers/movies_controller.rb
class MoviesController < ApplicationController
def index
@movies = if params[:movie]
Movie.joins(:categories).where(categories: { id: params[:movie][:category_ids] })
else
Movie.all
end
end
...
Essentially, wrapping the checkboxes inside a form, then tweak index
action when filter params exists.
Note: I'm not familiar with slim syntax, so tweak it if you get syntax error :).
answered Nov 27 '18 at 23:00
kasperitekasperite
2,03411016
2,03411016
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without theform_for
, but will probably end up using it anyway.)
– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
add a comment |
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without theform_for
, but will probably end up using it anyway.)
– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without the
form_for
, but will probably end up using it anyway.)– Josien
Nov 28 '18 at 13:05
Thanks for your answer, this gives me some new ideas! (I was trying to see if it could be done without the
form_for
, but will probably end up using it anyway.)– Josien
Nov 28 '18 at 13:05
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
other option is triggering page refresh upon clicking Filter link, you will need some javascript for that to work though.
– kasperite
Nov 28 '18 at 20:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53508717%2fhow-to-get-the-checked-boxes-with-link-to-into-params%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
Try
params.require(:movie).permit(:name, :rating, :category_ids => )
– barmic
Nov 27 '18 at 22:52