Table with list of fields - ruby-on-rails

I'm trying to put together a table with seven columns for a schedule. On each column I want to list twenty fields. I playing around but I cannot find the way to make it work.
Controller:
def new
#doctor = Doctor.new
140.times { #doctor.schedules.build }
end
Model:
has_many :schedules
def schedule_attributes=(schedule_attributes)
schedule_attributes.each do |attributes|
schedules.build(attributes)
end
end
Form:
<tr>
<% #doctor.schedules.each_with_index do |schedule, i| %>
<td>
<% if i > 0 && i % 20 == 0 %>
</td>
<td>
<% end %>
<%= fields_for "doctor[schedule_attributes][]", schedule do |schedule_form| %>
<ul>
<% schedule_form.text_field :day, value: #days[0] %>
<li><%= schedule_form.check_box :hour, value: "8:00" %></li>
</ul>
<% end %>
</td>
<% end %>
</tr>
This only outputs forty fields. The idea is output 140 fields, twenty in each column.
I would like to insert the twenty fields in one single cell. Can somebody point me on the right direction?

Using a simple (quick and dirty) approach, you can do this:
<tr>
<td>
<% #doctor.schedules.limit(140).each_with_index do |schedule, i| %>
<% if i > 0 && i % 20 == 0 %>
</td>
<td>
<% end %>
<%= fields_for "doctor[schedule_attributes][]", schedule do |schedule_form| %>
<ul>
<% schedule_form.text_field :day, value: #days[0] %>
<li><%= schedule_form.check_box :hour, value: "8:00" %></li>
</ul>
<% end %>
<% end %>
</td>
</tr>
If you want to reuse this logic, a helper method should be used:
def to_columns(collection, num_columns)
html = ""
count = collection.size
num_rows = (count / num_columns.to_f).ceil
collection.each_with_index do |item, i|
html << '<td>'.html_safe if (i % num_rows == 0)
html << yield(item, i)
html << '</td>'.html_safe if (i % num_rows == 0 || i == (count - 1))
end
html
end
And in your view, let this method make your <td> tags as needed:
<tr>
<%= to_columns(#doctor.schedules.limit(140), 7) do |schedule, i| %>
<%= fields_for "doctor[schedule_attributes][]", schedule do |schedule_form| %>
<ul>
<% schedule_form.text_field :day, value: #days[0] %>
<li><%= schedule_form.check_box :hour, value: "8:00" %></li>
</ul>
<% end %>
<% end %>
</tr>

Related

Aggregation on Many to many relations activerecord

I want to make ES aggregation on many to many activerecord model on rails but no way
My model :
class Foo < ApplicationRecord
searchkick
def search_data
{
bar: bar
}
end
has_many :bars
end
I have tried many solution but always get
{"active"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[]}}
The answer is add this :
def search_data
Attributes.merge(
bar_names: bar.map(&:name)
)
end
Here more about how to work with associated models, here is:
Item.rb (model):
# Relations
belongs_to :brand
## many-to-many relations between Items and Textures
has_many :item_attribute_for_textures
has_many :textures, through: :item_attribute_for_textures, :class_name => 'ItemAttribute::Texture'
# Searchkick
searchkick
def search_data
{
full_item_title: full_item_title,
brand: brand.try(:title),
texture: textures.map(&:title)
}
end
ItemController.rb:
def index
args = {}
args[:brand] = params[:brand] if params[:brand].present?
args[:texture] = params[:texture] if params[:texture].present?
query = params[:busca].presence || "*"
#items = Item.search query, where: args,
aggs: {
full_item_title: {},
brand: {},
texture: {}
}
end
and view index.html.erb:
<div class="row">
<div class="col-sm-2" style="font-size: 0.75rem">
<h5>Brands</h5>
<div>
<% #items.aggs["brand"]["buckets"].sort_by{ |b| b["key"] }.each do |bucket| %>
<div>
<% if params[:brand] == bucket["key"].to_s %>
<strong><%= bucket["key"] %></strong>
<% else %>
<%= link_to bucket["key"], request.params.merge(brand: bucket["key"]) %>
<% end %>
(<%= bucket["doc_count"] %>)
</div>
<% end %>
</div>
<hr>
<h5>Texture</h5>
<div>
<% #items.aggs["texture"]["buckets"].sort_by{ |b| b["key"] }.each do |bucket| %>
<div>
<% if params[:texture] == bucket["key"].to_s %>
<strong><%= bucket["key"] %></strong>
<% else %>
<%= link_to bucket["key"], request.params.merge(texture: bucket["key"]) %>
<% end %>
(<%= bucket["doc_count"] %>)
</div>
<% end %>
</div>
<hr>
</div>
</div>

Two submission forms with form-tag on same page in rails

I have a weekly time entry form currently. How can i have another time entry form on the same page ? and these two forms need be submitted separately as different records. Any help would be appreciated .
Here is my code:-
show_weeks.html.erb
<div class="table-responisve>
<% if #dates != nil %>
<table class="table-bordered">
<thead>
<tr>
<% #dates.each do |date| %>
<th><%=date.to_s+","+date.strftime("%A").to_s %> </th>
<% end %>
</thead>
<tbody>
<tr>
<% #dates.each do |date| %>
<% if #time_entry %>
<td><%= text_field_tag "#{date}", #time_entry.hour_per_day["#{date}"], class: "dates" %></td>
<% else %>
<%if date < Date.today %>
<td> <%= text_field_tag "#{date}", "", class: "dates" %> </td>
<%else %>
<td><%= text_field_tag "#{date}", "", class: "dates" if date == Date.today && Time.now.strftime("%H").to_i >= 10 %> </td>
<% end %>
<% end %>
<% end %>
</tr>
<tr>
<% if #time_entry %>
<td colspan="2"> Please Enter your Comment </td>
<td colspan="5">
<% #time_entry.comments.each do |c| %>
<p><%= text_field_tag "Comment", c.message %> </p>
<% end %>
</td>
<%else%>
<td colspan="2"> Please Enter your Comment </td>
<td colspan="5"><%= text_field_tag "Comment", "" %>
</td>
<%end%>
</tr>
</tbody>
</table>
</div>
<button type="button" class="btn btn-primary"
id="save_entries">Submit</button>
<%= form_tag save_time_entries_path, method: 'post',
id:"save_time_entries" do %>
<%= hidden_field_tag "start_date", #dates.first%>
<%= hidden_field_tag "end_date", #dates.last%>
<%= hidden_field_tag "total_hours", "" %>
<%= hidden_field_tag "project_id", #project.id %>
<%= hidden_field_tag "time_entry", "" %>
<%= hidden_field_tag "message", "" %>
<% if #time_entry%>
<%= hidden_field_tag "time_entry_detail_id", #time_entry.id %>
<% end %>
<% end %>
<script>
$("#save_entries").click(function(){
var time_entry = []
var hours = 0;
var message = document.getElementById("Comment").value;
$('.dates').each(function() {
hours += Number($(this).val());
if ($(this).val() == 0)
{
time_entry.push($(this).attr('name'),0)
}else{
time_entry.push($(this).attr('name'),$(this).val())
}
});
if (hours > 60) {
alert("Total Hours Should be equal to 60");
return false;
}
else {
$("#message").val(message);
$("#time_entry").val(time_entry);
$("#total_hours").val(hours);
$("#save_time_entries").submit();
}
})
</script>
<%end%>
you can create 2 different forms with form_for, it shouldn't be a problem
I figure out solution. Using of two submission forms on same page is bad idea. So I have create previous weeks records by using after create call back and i given default values for TimeEntryDetail. Here it creates all weeks records in background. Suppose if you skipped for 6 weeks , it'll create 6 empty records in background. Here is my working code
Modal.rb
after_create :chceck_time
def check_time
#project_mem_check= ProjectMember.where(project_id: project, member_id: user).first.created_at
#present week dates coming here |
#time_entry_check = TimeEntryDetail.where(project_id: project, user_id: user).first.created_at
if #time_entry_check.nil?
#time_entry_check=#project_mem_check
end
#last_week_start=Date.today.beginning_of_week
#last_week_end =Date.today.end_of_week
#cheking project member creation shall be less than time entry creation of that project.
if #project_mem_check <= #time_entry_check
#time_check_first=TimeEntryDetail.where(project_id: project, user_id: user).pluck(:start_date).first.to_date
#time_check_last =TimeEntryDetail.where(project_id: project, user_id: user).pluck(:start_date).last.to_date
#check first entry == last time entry
if #time_check_first==#time_check_last
pm=ProjectMember.where(project_id: project, member_id: user).first.created_at.to_date
tm=Date.today.to_date
unfilled_weeks= ((tm-pm)/7).to_i+1
unfilled_weeks.times {
#last_week_start= #last_week_start-7
#last_week_end = #last_week_end-7
#call_back_entry=TimeEntryDetail.create(start_date: (#last_week_start).to_date, end_date: (#last_week_end).to_date,
project_id: project.id, user_id: user.id, hours: 0, aasm_state: "pending", hour_per_day: "" )
#call_back_entry.comments << Comment.create(message: "Please fill the timesheet ")
}
else
#Already this user have time entries and create empty records from where he stopped.
pm=created_at.to_date #--.present time entry is coming
tm=Date.today.to_date
unfilled_weeks= ((tm-pm)/7).to_i+1
unfilled_weeks.times{
#last_week_start= #last_week_start-7
#last_week_end = #last_week_end-7
#call_back_entry=TimeEntryDetail.create(start_date: (#last_week_start).to_date, end_date: (#last_week_end).to_date,
project_id: project.id, user_id: user.id, hours: 0, aasm_state: "submitted", hour_per_day: "" )
#call_back_entry.comments << Comment.create(message: "Not yet filled this week ")
}
end
end
end

Kaminari - can't get dynamically selected per page limit

Here is my code.
Controller:
def bisac_main_counts
q = "
MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000')
WITH b, size((b)<-[:INCLUDED_IN]-()) as wokas_count
RETURN b.bisac_code as bisac_code, b.bisac_value as bisac_value, wokas_count
ORDER BY b.bisac_code
;"
#result = Neo4j::Session.current.query(q).to_a
if params[:limit]
#result = Kaminari.paginate_array(#result).page(params[:page]).per(params[:limit])
else
#result = Kaminari.paginate_array(#result).page(params[:page])
end
end
View:
<%= render 'home/main_links' %>
<%= paginate #result %>
<%= render 'bisac_counts' %>
<%= form_tag bisac_main_counts_path, method: :get do %>
<%= select_tag :limit, options_for_select([5, 10, 15, 20, 25], selected: params[:limit] || 25) %>
<% end %>
Partial:
<table>
<thead>
<tr>
<th>Bisac Code</th>
<th>Bisac Value</th>
<th>Wokas Count</th>
</tr>
</thead>
<tbody>
<% #result.each do |record| %>
<tr>
<td><%= record.bisac_code %></td>
<td><%= record.bisac_value%></td>
<td><%= record.wokas_count %></td>
</tr>
<% end %>
</tbody>
</table>
Apparently params[:limit] is not set in the view. It is always null. Changing pages is working.
What I am doing wrong here?
What was missing was a form for changing the limit and a way to trigger it.
Here are the changes (in the view only):
<%= render 'home/main_links' %>
<%= paginate #result %>
<%= render 'bisac_counts' %>
<%= form_tag bisac_main_counts_path, method: :get, id: 'limit_form' do %>
<%= select_tag :limit, options_for_select([5, 10, 15, 20, 25], selected: params[:limit] || 10) %>
<% end %>
<script type="text/javascript" >
$(function(){
$('#limit').change(
function() {
$('#limit_form').submit();
});
});
</script>

rails 4 passing a value to the controller

I have some radio buttons in show.erb.html and i want to pass the value of the selected one to the controller using a submit button or link_to
here is show.html.erb
<ol>
<% for poll_answer in #poll_question.poll_answers %>
<li>
<%= radio_button_tag "poll", poll_answer.content ,true%>
<%= h poll_answer.content %>
</li>
<% end %>
and here is the controller poll_questions_controller.rb
def calc
p = PollAnswer.find_by_content params[:poll]
m= p.counter
if m.nil == true
p.update_attributes counter: 1
else
m = 1+m
p.update_attributes counter: m
end
end
i tried link_to
<% link_to "click here ", controller: :poll_questions, action: :calc, :poll_questions => { :poll=> calc} %>
but it didn't work
Try form_tag helper and set your path
<%= form_tag calc_path do %>
<ol>
<% for poll_answer in #poll_question.poll_answers %>
<li>
<%= radio_button_tag "poll", poll_answer.content ,true%>
<%= h poll_answer.content %>
</li>
<% end %>
</ol>
<%= submit_tag "Submit" %>
<% end %>
In your route.rb file add the following
post 'calc' => 'poll_questions#calc', as: :calc

checkbox_tag check from comparing to database

In my users table, I have a column SkillType that has values "1", "2" and "3".
I want to check checkbox_tag in my view file when SkillType == "1" and I am trying the below given code:
def new
#user_basic=User.find(params[:id])
##user_education=Education.where(:UserID => params[:id])
end
And below given is my view file contents:
<td width="25%" align="left" style="vertical-align: bottom; color: #212121;" class="style6">
<%= check_box_tag :SkillType, "active", 1, true %>
<%= f.label(:SkillType, " Students") %>
</td>
But it is not checking the checkbox_tag.
You can check your checkbox in view label by this--
<%if #user_basic.SkillType == "1" %>
<%= check_box_tag :#user_basic.SkillType, :checked => true %>
<%end%>
In View
<%= check_box_tag :skill_type, :checked => get_status( #user_basic.SkillType) %>
In application_helper.rb
def get_status(skill_type)
skill_type.eql?("1") ? true : false
end
In view:
<% unless #user_basic.blank? %>
<% if #user_basic.SkillType.to_i == 1 %>
<%= check_box_tag 'Skilltype', #user_basic.id, 1, true %>
<% end %>
<% end %>
Hope that works !!

Resources