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>
Related
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
I have the following form which includes a hidden field tag, but when I look at the posted params, that hidden field param is not posting. Does anyone have any ideas on what could be wrong with the hidden field in this form?
<%=form_for '/schedule_path' do |f|%>
<% #total_hrs = 0 %>
<table>
<table class = "responstable" id ="form-table">
<tr>
<th>Employee</th>
<th>Job 1</th>
<th>Hrs</th>
<th>Job 2</th>
<th>Hrs</th>
<th>Job 3</th>
<th>Hrs</th>
<th>Total</th>
<th>Planned</th>
</tr>
<% #collectionsearch = "select jobs.id as id, jobs.name as name from jobs join (Select* from schedule_plans where schedule_plans." + #col + " >0 and schedule_plans.user_id =" + #userid.to_s + ") sp on sp.job_id = jobs.id order by name asc"
#collection = Hash[Job.connection.select_all(#collectionsearch).rows] %>
<% #schedule.each do |schedule| %>
<%
#hrs1 = schedule.hrs1.to_f
#hrs2 = schedule.hrs2.to_f
#hrs3 = schedule.hrs3.to_f
#search = "Select "+#col+" FROM employees where id= "+schedule.employee.id.to_s+" and user_id = "+current_user.id.to_s
#planned_hrs = Employee.connection.select_value(#search).to_f
#total_hrs = #total_hrs + #planned_hrs
%>
<% if schedule.employee.status && #planned_hrs > 0%>
<tr>
<%= f.fields_for :schedules, index: schedule.id do |sf| %>
<% #check = false %>
<td class = "large-col" > <%= schedule.employee.name %> </td>
<td> <%= if (schedule.job1.nil?)
sf.collection_select :job1, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job1, #collection, :first, :last, selected: schedule.job1
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs1.nil?)
sf.number_field :hrs1, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs1, value: #hrs1 , :step => 'any'
end
%>
</td>
<td > <%= if (schedule.job2.nil?)
sf.collection_select :job2, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job2, #collection, :first, :last, selected: schedule.job2
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs2.nil?)
sf.number_field :hrs2, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs2, value: #hrs2 , :step => 'any'
end
%>
</td>
<td > <%= if (schedule.job3.nil?)
sf.collection_select :job3, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job3, #collection, :first, :last, selected: schedule.job3
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs3.nil?)
sf.number_field :hrs3, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs3, value: #hrs3 , :step => 'any'
end
%>
</td>
<td class = "small-col" > <%= #hrs1+ #hrs2 + #hrs3 %> </td>
<td class = "small-col" > <%= #planned_hrs %> </td>
<% end %>
</tr>
<% end %>
<%end %>
<% if #total_hrs == 0 %>
<p class = "alert-danger"> Alert -You have not defined employee availability. Please go to Labor Management > Employees and define working hours for each employee. </p>
<%end%>
</table>
<%= f.hidden_field_tag :date, #date %>
<%= f.submit "Add", class: "btn btn-primary" %>
<% end %>
Parameters:
Processing by SchedulesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OjqNrvs3JWbtbs01XA3qnYk4Mv1+OstxcWCu3xDKo7NrN9GeHTsfK+VC1CGAC2S909NiPLofOXgEO5ES/kvB1Q==", "/schedule_path"=>{"schedules"=>{"391"=>{"job1"=>"1", "hrs1"=>"7.0", "job2"=>"4", "hrs2"=>"3.0", "job3"=>"", "hrs3"=>""}, "392"=>{"job1"=>"1", "hrs1"=>"3.0", "job2"=>"1", "hrs2"=>"4", "job3"=>"", "hrs3"=>""}}}, "commit"=>"Add"}
I am using the same hidden_tag field in this other form, and it works fine:
<%= form_for(#user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :username, #user.username %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
I thought I had tried this before, but right after submitting the question I gave it one last chance:
I changed
<%= f.hidden_field_tag :date, #date %>
<%= hidden_field_tag :date, #date %>
amd it started working.
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 !!
I am trying to use will_paginate for a list of items I want to display.
There is a form with drop down selections to fetch objects by status.
This is my controller
def list
#person = Person.find_by_id(session[:person_id])
params[:status] = params[:redirect_status] if params.has_key?('redirect_status')
#statuses = Peptide.statuses
#status = 'Not Ordered'
#status = params[:status] if params[:status] != nil || params[:status] == ''
#peptides = Peptide.find(:all, :conditions => ['status = ?', #status]).paginate(:per_page => 30, :page => params[:page])
if Peptide.count.zero?
flash[:notice] = "There are not any peptides entered"
redirect_to :action => 'new'
end#if zero
end
This is in the view
<form action="/peptide/create_spreadsheet_of_not_ordered" enctype="multipart/form-data" method="post">
<table class="sortable" cellpading="5" cellspacing="2" width="100">
<tr class= "header-line">
<th>SS</th>
<th>Status</th>
<th>Peptide</th>
<th>Gene</th>
<th>Submitter</th>
<th>Created</th>
<th>Updated</th>
<th>Sequence</th>
<th>Modification</th>
<th>Vendor</th>
</tr>
<% for #peptide in #peptides.reverse %>
<tr valign = "top" class= "<%= cycle('color_one', 'color_two') %>">
<!--an error here during development likely indicates that the people table has not be repopulated or
that no submitter primer is present for a primer-->
<td sorttable_customkey = 0 > <%=check_box_tag "box[]", #peptide.id %>
<td><%= #peptide.status%></td>
<td class><%= link_to #peptide.name, :action => :report, :id => #peptide.id %></td>
<td><%= gene_links(#peptide.genes) rescue 'Error'%></td>
<td><%= #peptide.submitter.name rescue "" %></td>
<td <%= sorttable_customkey_from_date(#peptide.created_at)%> >
<%= #peptide.created_at.strftime("%b %d %Y") rescue "Unknown"%>
</td>
<td <%= sorttable_customkey_from_date(#peptide.updated_at)%> >
<%= #peptide.updated_at.strftime("%b %d %Y") rescue "Unknown"%>
</td>
<td><%= #peptide.sequence%></td>
<td><%= #peptide.modifications%></td>
<td><%= #peptide.vendor%></td>
<%= buttons() %>
</tr>
<%end%>
<%= will_paginate #peptides %>
</table>
<br>
<%= submit_tag "Create Spreadsheet"%>
The default list is grouped by status ordered.
however when I select any other status and submit the form the pagination links take me back to the default status.
Please help me resolve this issue.
Thanks
Without seeing the view, it sounds like you need to add the current params as an argument to the links for the other statuses...
Update
Try adding the params to your status link:
<%= link_to #peptide.name, peptide_path(#peptide, params), :action => :report, :id => #peptide.id %>
The documentation is here.
I'm very new to rails and have this fairly basic question:
I have a form that takes in an array:
<td><%= fields_for "days" do |form| %>
M: <%= form.check_box "", {}, 'M', '' %>
T: <%= form.check_box "", {}, 'T', '' %>
W: <%= form.check_box "", {}, 'W', '' %>
Th: <%= form.check_box "", {}, 'Th', '' %>
F: <%= form.check_box "", {}, 'F', '' %>
<% end %>
This should be accessible through params[:days]. How can I assign params[:days] to a variable within the controller? #days is not correct here, right? (There's no Days object, so there's no instance of that object). What should go in the [correct_variable] slot below?
[correct_variable] = params[:days]
Thanks!
In response to comments:
I tried using #days, but for some reason it wouldn't get called where I'd like it to. In particular, I'd like to pass it to my Search model:
class Search < ActiveRecord::Base
attr_accessible :name, :day, :units, :instructor
def courses
#courses ||= find_courses
end
private
def find_courses
Course.find(:all, :conditions => conditions)
end
def day_conditions
["courses.day LIKE ?", "%"+ #days.join("%")+"%"]
end
I first instantiate #days in my courses controller (which is connected, through the index method, to a partial that uses an instance of the Search object.
More code:
From courses/index.html.erb:
<% if #search.save %>
<div id = "results_scroll">
<%= render :partial => 'searches/search_results' %>
</div>
<% else %>
From search_results partial:
<% "Search" %>
<table>
<tr>
<th align="left" width="25%"><%= 'Name' %></th>
<th align="left" width="10%"><%= 'Number' %></th>
<th align="left" width="20%"><%= 'Instructor' %></th>
<th align="left" width="10%"><%= 'Room' %></th>
<th align="left" width="5%"><%= 'Day' %></th>
<th align="left" width="5%"><%= 'Units' %></th>
<th align="left" width="15%"><%= 'Time' %></th>
<th align="left" width="10%"><%= 'Limitations' %></th>
</tr>
<%= render :partial => #search_show.courses %>
</table>
From the courses controller (this has the #search_show variable).
def index
#courses = Course.order(sort_column + " " + sort_direction)
#search = Search.new
#search_show = Search.last
#title = "List"
#days = params[:days]
Finally, the _course.html.erb partial:
<tr>
<td><%= course.name %></td>
<td><%= course.number %></td>
<td><%= course.instructor %></td>
<td><%= course.room %></td>
<td><%= course.day %></td>
<td><%= course.units %></td>
<td><%= course.time %></td>
<td><%= course.limitations %></td>
<td><%= link_to 'Show', course %></td>
</tr>
This is not really an answer, but there is some problems with how you are trying to access your #days variable you set in the controller.
# your controller
...
def index
...
#days = params[:days]
# your model
...
private
def day_conditions
["courses.day LIKE ?", "%"+ #days.join("%")+"%"]
end
The #days, in your model, you are calling is not going to access the #days in your controller. Each #days belongs to their respective instances.
You should find a method of sending the #days from your controller into your models, instead of using global variables.
Solved the problem by using a $days global variable instead of #days.