ajahongir / ajax-datatables-rails-v-0-4-0-how-to

ajax-datatables-rails v-0-4-0 sample project code
12 stars 6 forks source link

How can I add/include other parameter that can be read inside test_datatable.rb ? #1

Open aldrienht opened 8 years ago

aldrienht commented 8 years ago

First I tried to clone this repo, and successfully run it without error. Aside from I change the branch: to 'v-0-4-0'.

Question #1

For example I need to may if statement that checks the session role etc.

in controller index: something like:

respond_to do |format|
  format.html
  format.json { render json: ApplicantDatatable.new(view_context, session: session[:role]) }
end

After that, how can I get the session parameter inside ?

class TestDatatable < AjaxDatatablesRails::Base

Question #2 I'm getting some error when try to seach (i didn't implement the individual search per column, ONLY the default search).

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR) LIKE '%a%' OR CAST(applicants.name AS VARCHAR) LIKE '%a%') OR CAST(' at line 1: SELECT COUNT() FROM (SELECT applicants._ FROM applicants WHERE applicants.status = 'In-progress' AND (((((((((CAST(applicants.profile_file_name AS VARCHAR) LIKE '%a%' OR CAST(applicants.name AS VARCHAR) LIKE '%a%') OR CAST(applicants.project_id AS VARCHAR) LIKE '%a%') OR CAST(applicants.gender AS VARCHAR) LIKE '%a%') OR CAST(applicants.birth_date AS VARCHAR) LIKE '%a%') OR CAST(applicants.age AS VARCHAR) LIKE '%a%') OR CAST(applicants.country_id AS VARCHAR) LIKE '%a%') OR CAST(applicants.marital_status AS VARCHAR) LIKE '%a%') OR CAST(applicants.last_academic AS VARCHAR) LIKE '%a%') OR CAST(applicants.religion_id AS VARCHAR) LIKE '%a%')) AS foo _

Please help to figure it out.

Thank you!

ajahongir commented 8 years ago

look at here - https://github.com/ajahongir/ajax-datatables-rails/blob/v-0-4-0/lib/ajax-datatables-rails/base.rb#L10 init method already accepts options. so you can any time access to @options[:session]

aldrienht commented 8 years ago

Hi @ajahongir , thank you very much! my question #1 was solved. what does it mean having the error above when trying to search using the default search bar of dataTable, as in question #2 ?

ajahongir commented 8 years ago

well, in how-to sample project I am having same query: SELECT COUNT(*) FROM (SELECT cities.*, timezone AS custom_column FROM "cities" WHERE (((("cities"."id" = 0 OR CAST("cities"."name" AS VARCHAR) LIKE '%a%') OR custom_column LIKE 'a%') OR CAST("cities"."iata" AS VARCHAR) LIKE '%a%') OR "cities"."country_id" = 1) GROUP BY cities.country_id) AS foo'

and works well.

you can try to override counting method like:

  class TestDatatable < AjaxDatatablesRails::Base
    def as_json(options = {})
      {
        recordsTotal: get_raw_records.count(:all),
        recordsFiltered: filter_records(get_raw_records).count(:all),
        data: data
      }
    end
  end