Updating table fields with button click - ruby-on-rails

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 %>

Related

Undefined Method map for housework: String acts_as_taggable_on

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

How to update a record from index in rails

I was trying to update a User record (only the "closed" named column) from the user index's view. This is what I tried. But, it is not working. I used Devise gem to generate views and controllers. But, whatever, How can I update a records one particular value, directly from index(without going to edit's view). It would be very helpful, if someone would help me.
<% #users.each do |user| %>
<tr>
<td> <%= user.last_name %></td>
<td> <%= user.telephone %></td>
<td> <%= form_for user do |f| %>
<% if user.closed %>
<%= f.hidden_field :closed, value: false %>
<%= f.submit "Activate" %>
<% else %>
<%= f.hidden_field :closed, value: true %>
<%= f.submit "Deactivate" %>
<% end %>
<% end %> </td>
<%= link_to 'Remove', user_destroy_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Controller:
def user_index
if current_user.admin?
if params[:user_search].present?
#users = User.search_user(params[:user_search]).super_admin
else
#users = User.super_admin
end
else
if params[:user_search].present?
#users = User.search_user(params[:user_search]).admin
else
#users = User.admin
end
end
respond_to do |format|
format.html
format.csv { send_data User.to_csv, type: 'text/csv', filename: "users.csv" }
end
end
here is my controller. Actually there is also something done to search the users
In your controller, create two methods activate and deactivate.
In controller,
def activate
#user = User.find_by(:id=>params[:id])
#user.update(:closed=>false)
redirect_to #your path
end
def deactivate
#user = User.find_by(:id=>params[:id])
#user.update(:closed=>true)
redirect_to #your path
end
Set your routes as,
patch '#yourcontroller/:id/activate',to:'#yourcontroller#activate' , as: :activate
patch '#yourcontroller/:id/deactivate',to:'#yourcontroller#deactivate' ,as: :deactivate
and finally do changes in your view as,
<% #users.each do |user| %>
<tr>
<td> <%= user.last_name %></td>
<td> <%= user.telephone %></td>
<td> <%if user.closed?%>
<%= link_to 'activate',activate_path(id:user.id), method: :patch%>
<%else%>
<%= link_to 'deactivate',deactivate_path(id:user.id), method: :patch%>
<% end -%> </td>
<%= link_to 'Remove', user_destroy_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Its helpful pls up-vote!

How to use an if statement to filter records from a model object ror

I have service and servicebooking models, I want to view the current users made servicebookings by checking the owner_id attribute in the servicebooking model.
My servicebooking controller method:
def myservicebookings
if current_user.id == #servicebooking.owner_id
#servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
else
"You have no service bookings"
end
end
My servicebookings view:
<% if current_user.id == #servicebooking.owner_id %><% #servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
Currently I get the following error when trying to load the myservicebookings form:
undefined method `owner_id' for nil:NilClass
Any ideas on how to change this code to make it work? thanks in advance guys.
in you case, you dont need if statment because in this variable you have all current user servicebooking:
def myservicebookings
#servicebookings = current_user.servicebookings.
^^^^^^^^^^^^ search(params[:search]).
order(sort_column + " " + sort_direction).
paginate(:per_page => 5, :page => params[:page])
end
use this:
<% if #servicebookings.any? %>
<% #servicebookings.each do |servicebooking| %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
or this:
<% if #servicebookings.any? %>
<% for servicebooking in #servicebookings %>
<tr>
<td><%= servicebooking.date %></td>
<td><%= servicebooking.time %></td>
<td><%= servicebooking.service_name %></td>
</tr>
<% end if current_user.id == servicebooking.owner_id %>
<% else %>
<%= "You have no outgoing service bookings" %>
<% end %>
You should switch the position of the loop and and if statement in your view, then use the loop variableservicebooking not the instance variable #servicebooking, making it look like this
<% #servicebookings.each do |servicebooking| %>
<% if current_user.id == servicebooking.owner_id %>
the rest of the view ...
UPDATE: If you want to update on the controller level, then it's almost the same:
#servicebookings = current_user.servicebookings.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
#servicebookings.select! { |servicebooking| servicebooking.owner_id == current_user.id }
I would not mess with my views or my controller. I would go OOPS here. In RoR, its skinny controllers, fat models.
Goto your model,
class ServiceBooking < ActiveRecord::Base
# u = User, srch = params[:search],p = params[:page],p_p = per_page,s_c =sort_columns, s_d = sort_direction
def self.mine(u,srch,s_c,s_d,p,p_p = 5)
u.servicebookings.search(srch).order(s_c + " " + s_d).paginate(per_page: p_p, page: => p)
end
end
In your controller,
def myservicebookings
# 6th argument to this method is overridable.
#mine = ServiceBooking.mine(current_user,params[:search],sort_column,sort_direction,params[:page])
end
In your View,
<% #mine.each do |m| %>
<tr>
<td><%= m.date %></td>
<td><%= m.time %></td>
<td><%= m.service_name %></td>
</tr>
<% end %>

Displaying events for a specific user in a view ruby on rails

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 %>

rails 3.2 ajax live search

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

Resources