undefined method `some_method' for "some_variable":String. Ruby on Rails - 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.

Related

Differentiate Past Events from Future Events

QUESTION: How do I show events that have start_time<time.now & start_time>time.now in two different tables?
I have scaffold event start_time:datetime title
Events_controller:
def index
#event = Event.all
end
index.html.haml:
%table
%thead
%tr
%th Start_time
%th Title
%tbody
- #events.each do |event|
%tr
%td= event.start_time
%td= event.title
thank you:)
You may simply define scopes for the past and future events:
class Event < ActiveRecord::Base
scope :in_the_past, { where('start_time < ?', Time.now }
scope :in_the_future, { where('start_time > ?', Time.now }
end
Then your views will be:
_events_table.html.haml
%table
%thead
%tr
%th Start_time
%th Title
%tbody
- events.find_each do |event|
%tr
%td= event.start_time
%td= event.title
index.html.haml
/ Past events
= render 'events_table', events: Event.in_the_past
/ Future events
= render 'events_table', events: Event.in_the_future
Use Enumerable#partition to divide your events into buckets.
past, future = Event.all.partition { |e| e.start_time < Time.now }
This could be a possible solution. with two query...
Controller
def index
#upcoming_events = Event.where("start_time > ?", Time.now)
#past_events = Event.where("start_time < ?", Time.now)
end
View
%table
%thead
%tr
%th Start_time
%th Title
%tbody
- #upcoming_events.each do |event|
%tr
%td= event.start_time
%td= event.title
%table
%thead
%tr
%th Start_time
%th Title
%tbody
- #past_events.each do |event|
%tr
%td= event.start_time
%td= event.title
If you are not bothering about performance and you're ok with 2 SQL queries, then I'd suggest something like that:
def index
#past_events = Event.where('start_time < ?', Time.zone.now)
#future_events = Event.where('start_time > ?', Time.zone.now)
end

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')

Sorting with clicking on a button

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'

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