ruby on rails - Filter in list page associated with other model -
using rails 4 , ruby 2.2, have subject list page need add filter subject's name, username, active , books associated subject. 3 model i.e
subject, book , user
are associated below:
user.rb
class user < activerecord::base has_many :subjects, dependent: :destroy has_many :books, dependent: :destroy end
book.rb
class book < activerecord::base belongs_to :subject, dependent: :destroy belongs_to :user, dependent: :destroy has_many :images, dependent: :destroy end
subject.rb
class subject < activerecord::base belongs_to :user has_many :books validates :name, presence: true, length: { minimum: 3 } def self.names(name) subject = subject = subject.where("name ?", "%#{name}%") return subject end def self.actives(active) subject = subject = subject.where("active = ?", "%#{active}%") return subject end def self.usernames(username) user = user = user.where("username ?", "%#{username}%") return user end end
now trying make filter on subject list page following column
- username(drop down user table)
- subject name(text field subject table)
- active(drop down subject table)
- has books (check box, association books, if checked display subjects having books)
i have index subject follows:
subjects_controller.rb
def index @subjects = subject.page(params[:page]).per(10) @users = user.all @subjects = @subjects.names(params[:name]) if params[:name].present? @subjects = @subjects.actives(params[:active]) if params[:active].present? @users = @users.usernames(params[:username]) if params[:username].present? end
and
index.html.erb
as:
<h1>listing subjects</h1> <%= link_to 'new subject', new_subject_path %> <%= form_tag subjects_path, :method=> "get" %> <%= label_tag :username, "username" %> <%= select_tag :username, options_from_collection_for_select(user.all, "id", "username", username) %> <%= label_tag :name, "subject name"%> <%= text_field_tag :name %> <%= label_tag :active, "active" %> <%= select_tag :active, options_for_select([['active', true], ['inactive', false]]) %> <%= submit_tag "submit" %> <%= link_to "clear search", request.path, class:"cancel-button" %> <% end %> <div id="subjects"><%= render "index" %></div> <%= paginate @subjects %><br>
my console log as:
started "/subjects?utf8=%e2%9c%93&username=11&name=&active=false&commit=submit" ::1 @ 2016-04-19 10:20:06 +0530 processing subjectscontroller#index html parameters: {"utf8"=>"✓", "username"=>"11", "name"=>"", "active"=>"false", "commit"=>"submit"} user load (0.4ms) select `users`.* `users` `users`.`id` = ? order `users`.`id` asc limit 1 [["id", 11]] user load (0.6ms) select `users`.* `users` subject load (0.5ms) select `subjects`.* `subjects` (active = '%false%') limit 10 offset 0 rendered subjects/_index.html.erb (5.1ms) (0.5ms) select count(*) `subjects` (active = '%false%') rendered subjects/index.html.erb within layouts/application (34.9ms) completed 200 ok in 99ms (views: 94.1ms | activerecord: 2.1ms)
questions:
- how can have username column associated each book in filter list
- how make filter list has books or not radio button?
- how active subject(boolean datatype) list work ?
- how checkbox books availability can done, means, if checkbox marked need show subject having books.
let me know if need provide more details
Comments
Post a Comment