Sorting with clicking on a button - ruby-on-rails

I have something like this:
%section
%h1= t('.MyTableData')
.row-fluid
.span8
%table.table
%thead
%tr
%th= sortable('teachers', 'teacher_name', 'true')
%th= sortable('teachers', 'teacher_score')
%th= sortable('teachers', 'specialty')
%tbody
- #teachers.each do |teacher|
%tr
%td= as_full_name(teacher[:first_name], teacher[:last_name])
%td= number_to_percentage(provider[:teacher_score], precision: 0)
%td= provider[:specialty_name]
.span2
=render partial: 'search'
So it creates a table with some columns, if I click on the column headers like teacher_name, etc it will sort the table based on that using that sortable method.
Now I want to add a simple twitter bootstrap button saying "Reset Sorting", and when I click that it should reset these sorting we have done by clicking on column headers and go and sort it only by teacher_name.
I am very new to all this and can't put these pieces of puzzle together, so I need a button, and the sort method I need there is some samples of it in that sortable methods, But can't figure out how to put all these together and solve this.
def sortable(table, column, default_column = false)
table_params = params[table] || {}
same_sort_column = same_sort_column(table_params, column, default_column)
current_sort_direction = sort_direction(table_params)
new_direction = same_sort_column && current_sort_direction == 'asc' ? 'desc' : 'asc'
link_to(t(".#{column}"), params.merge(table => { sort_column: column, sort_direction: new_direction }))
end

= link_to 'Reset sorting', params.merge(:teachers => {:sort_column => 'teacher_name', :sort_direction => 'asc'}), :class => 'btn'

Related

Ruby on Rails - passing locals to partial inside each loop (With Haml)

i have an object array. i'm trying to loop through it and output results to a table. i want one of my table columns to be a rendered partial (form). but no matter in what row i submit it always sends me the locals of the last row output.
Is this normal behavior of rails or am i doing something wrong?
my view is :
%table.table.table-hover
%thead
%tr
%th User id
%th User email
%th Account name
%th Intercom Conversation
%tbody
- #users.each do |user|
%tr
%td= user.user_id
%td= user.user_email
%td= user.account_name
%td
- if user.intercom_conversation
%h4= user.intercom_conversation
= link_to('edit', '#', {class: 'edit'})
.conversation_edit
= render(partial: 'conversation_form', locals: {user_id: user.user_id, fault_id: #fault.fault_id})
my rendered partial is :
= form_tag('update_conversation', method: 'get')
= hidden_field_tag(:fault_id, fault_id)
= hidden_field_tag(:user_id, user_id)
yes
= radio_button_tag(:conversation, 0)
no
= radio_button_tag(:conversation, 1)
ignored
= radio_button_tag(:conversation, 2)
= submit_tag('Change')
i guess i should say all rendering (in browser are correct and no error is raised)
thanks in advance .
You're outputting a series of empty forms without closing tags. You need to pass a block to form_tag and indent the contents for it to automatically add the needed </form> tags.
= form_tag('update_conversation', method: 'get') do
= hidden_field_tag(:fault_id, fault_id)
= hidden_field_tag(:user_id, user_id)
yes
= radio_button_tag(:conversation, 0)
no
= radio_button_tag(:conversation, 1)
ignored
= radio_button_tag(:conversation, 2)
= submit_tag('Change')

Rails - Sortable and Highlighted Columns

I am trying to highlight a column title in Rails, I have followed the following tutorial to create ascending and descending sortable columns (http://railscasts.com/episodes/228-sortable-table-columns). However I can't make the higlighting working.
Here is the code I ve been using :
for the view :
%table#movies<br/>
%thead<br/>
%tr<br/>
%th{:id => 'title_header'}= sortable "title", "Movie Title"<br/>
%th Rating<br/>
%th= sortable "release_date", "Release Date"<br/>
%th More Info<br/>
the application helper :
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "hilite" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end
end
the controller :
def index
#movies = Movie.order(sort_column + " " + sort_direction)
end
and
def sort_column
Movie.column_names.include?(params[:sort]) ? params[:sort] : "title"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
and the stylesheet :
table#movies th.hilite {
background-color: yellow;
}
I can't find whats wrong, but when I click on the header it is only sorting and not highlighting in yellow... suspect that it has something to do with the *css_class*.
Thanks for your help!!
I believe it's because the class hilite isn't on the table header(<th>) element but on the anchor (<a>) element. Therefor in order to match the element, the CSS would need to be:
table#movies th a.hilite {
background-color: yellow;
}
So you either need to fix up your CSS or fix up which element you are putting the class on.
Thansks Link, got it, had to test the sorting before runing the sortable helper :
%table#movies
%thead
%tr
%th{params[:sort] == "title" ? {:class => "hilite"} : {}, :id => "title_header"}= sortable "title", "Movie Title"
%th Rating
%th{params[:sort] == "release_date" ? {:class => "hilite"} : {}, :id => 'release_date_header'}= sortable "release_date", "Release Date"
%th More Info
Thanks all for your answers!
Benjamin

undefined method `some_method' for "some_variable":String. Ruby on Rails

I'm trying to I'm trying to show table, but I get an error
undefined method `name' for "Sphere Kharkov":String
This is my Instance Variables:
#result1 {"No department"=>"1", "Dep2"=>"1", "Sphere Kharkov"=>"2"}
#result2 {"Sphere Kharkov"=>"1"}
#department_names ["Sphere Kharkov", "Dep2", "Dep without members", "No department"]
Controller's methods
def age_profile
#result1 = age_report(1,9)
#result2 = age_report(10, 12)
#department_names = current_company.departments.map(&:name)
#department_names << "No department"
end
def age_report(start_age, end_age)
result = {}
User.select("COALESCE(departments.name, 'No department') AS name, COALESCE(count( * ), '0') AS count")
.joins("LEFT JOIN departments ON departments.id = users.department_id")
.where(:users => { :is_deleted => 0, :company_id => current_company.id})
.where("TIMESTAMPDIFF(YEAR, `date_of_birth`, CURDATE()) BETWEEN :start_age AND :end_age", start_age: start_age, end_age: end_age)
.group("departments.name")
.each {|d| result[d.name] = d.count }
result
end
In View:
%table.table.table-striped.table-bordered
%tr
%th= t('Depatment')
%th= t('1-18')
%th= t('18-25')
%th= t('25-45')
-#department_names.each do |f|
%tr
%td= f.name
%td= #result1[f.name]
%td= #result2[f.name]
Сould you help me please. What's wrong?
#department_names variable contain strings that don't respond to name method. Simple solution is:
- #department_names.each do |f|
%tr
%td= f
%td= #result1[f]
%td= #result2[f]
This line:
#department_names = current_company.departments.map(&:name)
Retrieves only the names of the companies. So basically you are calling the name method on the result of the name method.
So either remove .map(&:name) or just use f insteaf of f.name.

How to correctly view such in rails?

I have selected for every model it's data from another table using this in controller:
def modelv
#model = Model.find(:all, :conditions => { :MOD_MFA_ID => params[:man]})
#ct1 = CountryDesignation.all(:conditions => { :CDS_ID => #model.map(&:MOD_CDS_ID)})
#ct = #ct1.uniq{|hh| hh.CDS_ID}
#destext = DesText.find(:all, :conditions => { :TEX_ID => #ct.map(&:CDS_TEX_ID)})
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #model }
end
end
sorry for dirty code.
and my view
%table %tr
%th Mfa id
%th Год начала выпуска
%th Год завершения выпуска
- #model.each do |model|
%tr
%td= model.MOD_CDS_ID
%td= link_to model.MOD_ID, model
%td= link_to model.MOD_PCON_START, model
-if model.MOD_PCON_END.blank?
%td= link_to "По настоящее время", model
-else
%td= link_to model.MOD_PCON_END, model
-#%td= model.country_designations.des_texts.TEX_TEXT
-#= link_to 'Show model', model %br %table %tr
- #destext.each do |t|
%tr
%td= t.TEX_ID
%td= t.TEX_TEXT
- #ct.each do |ct|
%tr
%td= ct.CDS_ID
%td= ct.CDS_TEX_ID
But how to display for every model its t.TEX_TEXT and ct.CDS_TEX_ID, not in another loop, but in same table, in column? And moreover, my method is fetching data, but when I select data, when ct.CDS_TEX_ID is same for some more than one entry, #destext is grouping, and i fetch only one instance, for example
cds_id cds_tex_id
110007626 420077
110007627 420077
and as result i get from #destext only one row. Help me please. It's non-standart db
See, table MODELS has field MOD_CDS_ID, with this field I go to table COUNTRY_DESIGNATIONS and it's field CDS_ID = MOD_CDS_ID (from MODELS table), than from this table I select field CDS_TEX_ID and go with this id to table DES_TEXTS it's field TEX_ID must be equal from previous table CDS_TEX_ID, and fetch field TEX_TEXT !!! And this field in my iteration #model.each do.... i must display for every model

How to select in rails method using previous find array?

I want to find array, but gow can i do this?
#model = Model.find(:first, :conditions => { :MOD_MFA_ID => params[:man]})
#ct = CountryDesignation.find(:first, :conditions => { :CDS_ID => "110000002"})
but :CDS_ID => "110000002" is not good. I need to select it via #model.Field, for example: :CDS_ID => #model.Field. But also #model is not just one entry, it's an array. So I need for every Model select CountryDesignation.
But then I need to select from CountryDesignation array DesText array
#destext = DesText.find(:all, :conditions => { :TEX_ID => #ct.Field})
How to do this work?
And how to correct view this?
%table
%tr
%th Mfa id
%th Год начала выпуска
%th Год завершения выпуска
- #model.each do |model|
%tr
%td= link_to model.MOD_ID, model
%td= link_to model.MOD_PCON_START, model
-if model.MOD_PCON_END.blank?
%td= link_to "По настоящее время", model
-else
%td= link_to model.MOD_PCON_END, model
-#%td= model.country_designations.des_texts.TEX_TEXT
-#= link_to 'Show model', model
%br
- #destext.each do |t|
name
%td= t.TEX_ID
%td= t.TEX_TEXT
- #ct.each do |ct|
ct
%td= ct.CDS_ID
%td= ct.CDS_TEX_ID
You can use map. If the model attribute you're interested in were named cds_id:
#models = Model.all(:conditions => { :MOD_MFA_ID => params[:man] })
#ct = CountryDesignation.all(:conditions => { :CDS_ID => #models.map(&:cds_id) })
This essentially grabs the cds_id attribute of each model into an array and uses those values in a SQL IN clause. It returns an array of CountryDesignation objects that match.
What you're looking for is a way for ActiveRecord to handle the array in the same manner as an IN clause in SQL. Luckily, AR does that by default when passed an array. In other words, you should be able to write:
#model = Model.find(:first, :conditions => { :MOD_MFA_ID => })

Resources