如何将带有 link_to 的复选框放入参数中?
How to get the checked boxes with link_to into params?
我有一个带有电影和类别的 Rails 5.2.1 应用程序。它们通过 has_and_belongs_to_many
关系与连接 table.
相互连接
尝试执行以下操作:在 index
电影页面上,我想通过选中类别复选框来过滤显示的电影集合。我可以正确地显示类别的复选框,但是我很难获得关于哪些复选框被选中到参数中的信息。
/ 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
这些:category_ids
好像是错的。是否有可能以这种方式以某种方式获得复选框结果(以便使用查询字符串参数进一步过滤)?我错过了什么,例如在我的控制器中?
# 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
以上是一个示例应用程序,使用一些脚手架和一些编辑生成:
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
-> 添加 has_and_belongs_to_many :movies
到类别 class
-> 添加 has_and_belongs_to_many :categories
到电影 class
-> 在电影 class
中将 category_ids: []
添加到 movie_params
试试这个,看看它是否适合你。
查看:
/ 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
控制器:
# 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
...
本质上,将复选框包装在一个表单中,然后在过滤器参数存在时调整 index
操作。
注意:我不熟悉 slim 语法,所以如果你遇到语法错误,请调整它:)。
我有一个带有电影和类别的 Rails 5.2.1 应用程序。它们通过 has_and_belongs_to_many
关系与连接 table.
尝试执行以下操作:在 index
电影页面上,我想通过选中类别复选框来过滤显示的电影集合。我可以正确地显示类别的复选框,但是我很难获得关于哪些复选框被选中到参数中的信息。
/ 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
这些:category_ids
好像是错的。是否有可能以这种方式以某种方式获得复选框结果(以便使用查询字符串参数进一步过滤)?我错过了什么,例如在我的控制器中?
# 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
以上是一个示例应用程序,使用一些脚手架和一些编辑生成:
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
-> 添加 has_and_belongs_to_many :movies
到类别 class
-> 添加 has_and_belongs_to_many :categories
到电影 class
-> 在电影 class
category_ids: []
添加到 movie_params
试试这个,看看它是否适合你。
查看:
/ 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
控制器:
# 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
...
本质上,将复选框包装在一个表单中,然后在过滤器参数存在时调整 index
操作。
注意:我不熟悉 slim 语法,所以如果你遇到语法错误,请调整它:)。