I'm still a Rails-Learner and getting desperate about implementing an ajax live search. The search seems to work on submitting, but not on keyup. Can't figure out why...
index.html.erb
<%= form_tag contacts_path, :method => 'get', :id => "contacts_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="cresults_div"><%= render 'cresults' %></div>
<% end %>
<%= link_to 'New Contact', new_contact_path %>
contacts_controller.rb
def index
#contacts = Contact.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
index.js.erb
$("#cresults_div").html("<%= escape_javascript(render("cresults")) %>");
contact.rb
def self.search(search)
if search
where('last_name LIKE ?', "%#{search}%")
else
scoped
end
end
contacts.js.coffee
jQuery ->
# Ajax search on submit
$('#contacts_search').submit( ->
$.get(this.action, $(this).serialize(), null, 'script')
false
)
# Ajax search on keyup
$('#contacts_search input').keyup( ->
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, 'script')
false
)
_cresults.html.erb
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<h1>Listing contacts</h1>
<table>
<tr>
<th>Salutation</th>
<th>First name</th>
<th>Last name</th>
<th>Stree</th>
<th>Street no</th>
<th>Zip</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Phone</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #contacts.each do |contact| %>
<tr>
<td><%= contact.salutation %></td>
<td><%= contact.first_name %></td>
<td><%= contact.last_name %></td>
<td><%= contact.stree %></td>
<td><%= contact.street_no %></td>
<td><%= contact.zip %></td>
<td><%= contact.city %></td>
<td><%= contact.state %></td>
<td><%= contact.country %></td>
<td><%= contact.phone %></td>
<td><%= contact.email %></td>
<td><%= link_to 'Show', contact %></td>
<td><%= link_to 'Edit', edit_contact_path(contact) %></td>
<td><%= link_to 'Destroy', contact, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
also tried to additional add
application.js
$(function() {
$("#contacts_search input").keyup(function() {
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, "script");
return false;
});
});
But the live search won't start on typing... why?
In this particular case I had to remove the
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
Block from the index-Method in the contacts controller
Related
I'm building a Task Manager in Rails. I used scope to get all instances of a Task model that are due today. The scope on the Task model works and is returning only Tasks due today when calling due_today on Tasks in the Rails console. But I can't seem to show these Tasks due today in the view. Do I need to add a conditional?
This is my views index.html.erb
<p id="notice"><%= notice %></p>
<h1>Team's Tasks</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<h1>Tasks due today.</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task', new_task_path %>
</p>
this is my Task model
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue, -> { where("duedate < ?", Time.now) }
#use scope on Task model to get only tasks that are due today.
scope :due_today, ->{ where("duedate >= ? AND duedate <= ?", Date.current.beginning_of_day, Date.current.end_of_day) }
end
This is the controller
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
# GET /tasks or /tasks.json
# Order all tasks by ascending order
def index
#tasks = Task.all.order(duedate: :asc)
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: "Task was successfully created." }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title, :description, :duedate, :completed)
end
end
I assume you have a TasksController and an index method there.
try following in the index method
def index
#tasks = Task.due_today
end
let me know if this works
You need to change due_date scope.. use between or date_trunc method.
where("date_trunc('day', duedate)::date = ?", Date.today)
let me know if it works
Got it to work. No changes to the controller. None needed. This is the code.
This is the Task model.
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue, -> { where("duedate < ?", Time.now) }
#use scope on Task model to get Tasks due today.
scope :due_today, ->{ where("duedate >= ? AND duedate <= ?", Date.current.beginning_of_day, Date.current.end_of_day) }
def overdue?
duedate < Time.now
end
def due_today?
duedate >= Date.current.beginning_of_day && duedate <= Date.current.end_of_day
end
end
This is the Task view.
<p id="notice"><%= notice %></p>
<h1>Team Task Manager</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<br>
<h1>DUE TODAY!</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task', new_task_path %>
</p>
Im using acts_as_taggabe gem for my rails todo app project. In my
index.html file, Im performing an array method map on task.tag_list,
task.tag_list is supposed to return me an array of tags which ill operate
on but im getting error. It works alright without the map method. It shows
all the tags as desired.
#index.html.erb
<% #tasks.each do |task| %>
<tr>
<td width="60%"><%= task.task %></td>
<td width="20%"><%= task.deadline %></td>
<td width="20%"><%= task.tag_list.map {|x| x + "testing"} %>
<td><%= link_to 'Show', task_path(task), class: 'button'%></td>
<td><%= link_to 'Edit', edit_task_path(task), class: 'button' %></td>
<td><%= link_to 'Delete ', task_path(task),
method: :delete,
data: { confirm: 'Are you sure?' }, class: 'button' %>
</td>
</tr>
<% end %>
#routes.rb
get '/tags/:tag', to: 'tasks#index', as: :tag
#tasks_controller
def index
if params[:tag]
#tasks = current_user.tasks.tagged_with(params[:tag])
else
#tasks = current_user.tasks
end
end
I'm updating a registry, but when the registry is updated, the row of the updated record is disorganized, here my code:
index.html.erb
<table id="enterprises-items">
<thead>
<tr>
<td>Nombre</td>
<td>Email</td>
<td>Categoria</td>
<td>Municipio</td>
<td class="text-left">...</td>
<td colspan="2">Opciones</td>
</tr>
</thead>
<tbody>
<% #enterprises.each do |enterprise| %>
<%= render partial: "enterprises/list", locals: { enterprise:enterprise } %>
<% end %>
</tbody>
</table>
update.js.erb
<% if #enterprise.errors.empty? %>
$('#exampleModal1').foundation('close');
$('#enterprise-<%= #enterprise.id %>').html("<%= escape_javascript(render partial: 'list', locals: { enterprise:#enterprise }) %>");
<% end %>
_list.html.erb
<tr id="enterprise-<%= enterprise.id %>">
<td><% if enterprise.name.blank? %>No disponible<% else %><%= truncate(enterprise.name, length: 30) %><% end %></td>
<td><% if enterprise.email.blank? %>No disponible<% else %><%= truncate(enterprise.email, length: 30) %><% end %></td>
<td><% if enterprise.enterprise_tag_id.blank? %>No disponible<% else %><%= truncate(enterprise.enterprise_tag_id, length: 10) %><% end %></td>
<td><% if enterprise.municipality_id.blank? %>No disponible<% else %><%= truncate(enterprise.municipality_id, length: 15) %><% end %></td>
<td class="text-left">...</td>
<td><%= link_to edit_enterprise_path(enterprise), remote: true, :data => { :open => 'exampleModal1' }, class: "button tiny green", id: "update_" do %><i class="fi-pencil"></i><% end %></td>
<td><%= link_to enterprise, method: :delete, data: { confirm: "¿Desea eliminar este registro?" }, remote: true, class: "button tiny red" do %><i class="fi-trash"></i><% end %></td>
</tr>
This is the solution =D
$("#enterprise-<%= #enterprise.id %>").replaceWith("<%= escape_javascript(render partial: 'list', locals: { enterprise:#enterprise }) %>");
I am using Rails 4 and devise. I am trying to use an if statement in my 'myevents' view to only display the events that belong to the currently logged in user, can anybody help, currently it returns all the events and ignores the if statement. I also tried putting the if statement in the controller but no success there, here is my code:
///////////Myevents view
<h1>My Events</h1>
<table>
<tr>
<th></th>
<th><%= sortable "name" %></th>
<th><%= sortable "date" %></th>
<th><%= sortable "time" %></th>
<th><%= sortable "description" %></th>
<th><%= sortable "dresscode" %></th>
<th><%= sortable "price" %></th>
</tr>
<% #events.each do |event| %>
<% if event.user_id == current_user %>
<tr>
<td><%= image_tag event.avatar.url %></td>
<td><%= event.name %></td>
<td><%= event.date %></td>
<td><%= event.time %></td>
<td><%= event.description %></td>
<td><%= event.dresscode %></td>
<td><%= event.price %></td>
<td><%= link_to "Show", event_path(event) %></td>
<td><%= link_to "Edit", edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event_path(event),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% else %>
<%= puts "No events to display" %>
<% end %>
<% end %>
</table>
///////routes.rb declaration
get 'myevents', to: 'events#myevents'
////controller action
def myevents
#events = Event.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
The way you're fetching Events is wrong. Your models should be setup as so:
class User
has_many :events
end
class Event
belongs_to :user
end
controller
def myevents
#events = current_user.events.search(params[:search).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
view
<% if #events.any? %>
# #events.each do |event|
<% else %>
# no events
<% end %>
I'm trying to update cell in my table with the data from a drop down box in that row each time the button is clicked in that row. Each row has a drop down box and a button as you can see in this image:
http://i.imgur.com/eVJumuk.png
I'm trying to set it up so that when user selects a value from a drop down box and clicks the update button it will update value of Room column only for that row. But I can't figure out how to get the button even working and wanted to see if anyone can help me with this.
Here is my controller:
def index
#students = Student.all
#first_floor = %w(1101 1102 1103 1104 1105)
#second_floor = %w(2101 2102 2103 2104)
#third_floor = %w(3101 3102 3103 3104)
#selected_room = params[:room]
respond_to do |format|
format.html # index.html.erb
format.json { render json: #students }
end
end
Here is the part of the view for the table:
<% #students.each do |student|%>
<tr>
<td><%= student.id %></td>
<td><%= student.n_number %></td>
<td><%= student.f_name %></td>
<td><%= student.l_name %></td>
<td><%= student.date_submit %></td>
<td><%= student.floor_pref %></td>
<td><%= #selected_room %></td>
<% form_tag do %>
<% if student.floor_pref == '1st' %>
<td><%= select_tag 'room', options_for_select(#first_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<% if student.floor_pref == '2nd' %>
<td><%= select_tag 'room', options_for_select(#second_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<% if student.floor_pref == '3rd' %>
<td><%= select_tag 'room', options_for_select(#third_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<td><%= submit_tag 'Update' %></td>
<% end %>
<td><%= button_to 'Show', :controller => 'students', :action => 'preview', :id => student%></td>
<td><%= button_to 'Remove', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
button_tag creates a form on your page. The problem that you have right now is that the select_tag dropdown is not part of that form. What you probably want to do is to create the form explicitly and have the dropdown be inside of it. Replace your last 2 td's with something like this:
<%= form_tag do %>
<% if student.floor_pref == '1st' %>
<td><%= select_tag 'room', options_for_select(#first_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<% if student.floor_pref == '2nd' %>
<td><%= select_tag 'room', options_for_select(#second_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<% if student.floor_pref == '3rd' %>
<td><%= select_tag 'room', options_for_select(#third_floor.map { |value| [value,value]}, #selected_room) %></td>
<% end %>
<td><%= submit_tag 'Update' %></td>
<% end %>