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 }) %>");
Related
i have three model project_manager, project_director, and human_resource each has a status Boolean field how can i print some thing in rails if Boolean value of these three model is true. currently i am accessing data from model by doing this-
<% if project_site.project_managers.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_managers.each do |project_manager| %>
<% if project_manager.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_manager.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.project_directors.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.project_directors.each do |project_director| %>
<% if project_director.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif project_director.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
<% if project_site.human_resources.empty? %>
<td class="pending fi-eye"><%= " Pending" %></td>
<% else %>
<% project_site.human_resources.each do |human_resource| %>
<% if human_resource.status == false %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% elsif human_resource.status == true %>
<td class="approved fi-check"><%= " Approved" %></td>
<% end %>
<% end %>
<% end %>
i want to print approved if all these three model status value is true how can i do that in rails?
Create a helper method and put the following code :
def check_resource_status(project_site, resources)
statuses = project_site.send(resources.to_sym).pluck(:status)
statuses.all? ? true : false
end
def status_container(status)
content_tag :div, class: ['sample'] do
status_label = status ? 'Approved' : 'Rejected'
default_class = status ? 'fi-check' : 'fi-x'
status_class = [default_class, status_label.downcase]
concat content_tag(:label, status_label, class: status_class)
end
end
From your view file :
status_container(check_resource_status(project_site, 'human_resources'))
status_container(check_resource_status(project_site, 'project_directors'))
status_container(check_resource_status(project_site, 'project_managers'))
If I understood your question correctly, all you need is something like this:
<% if human_resource.status? %>
<td class="approved fi-check"><%= ' Approved' %></td>
<% else %>
<td class="rejected fi-x"><%= ' Rejected' %></td>
<% end %>
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 am learning to use CRUD, and setup a page to add a record, however it only generated blank record? Can you take a look my code?
thanks!
here is the Page controller:
class PagesController < ApplicationController
def index
#test = Page.all
end
def new
#test = Page.new
end
def create
#test = Page.new(params.permit(:subject_id,:name,:position,:visible,:permalink))
if #test.save
flash[:notice] = "Page created successfully!"
redirect_to(:action => 'index')
else
render('new')
end
end
end
here is the new.html.erb
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="pages new">
<h2>Create Subject</h2>
<%= form_for(:Page, :url => {:action => 'create'}) do |f| %>
<table summary="Page form fields">
<tr>
<th>Subject_ID</th>
<td><%= f.text_field(:subject_id) %></td>
</tr>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
<tr>
<th>Permalink</th>
<td><%= f.text_field(:permalink) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Subject") %>
</div>
<% end %>
</div>
Here is index.html.erb
<div class="pages index">
<h2>Pages</h2>
# <%= link_to("Add New Page", {:action => 'new'}, :class => 'action new') %>
<table class="listing" summary="Page list">
<tr class="header">
<th>Position</th>
<th>Page Name</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #test.each do |f| %>
<tr>
<td><%= f.position %></td>
<td><%= f.name %></td>
<td class="center"><%= f.visible %></td>
<td class="center"><%= f.permalink %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => f.id}, :class => 'action show') %>
<%= link_to("Edit", {:action => 'edit', :id => f.id}, :class => 'action edit') %>
<%= link_to("Delete", {:action => 'delete', :id => f.id}, :class => 'action delete') %>
</td>
</tr>
<% end %>
</table>
</div>
Thanks so much!
Strong parameters
Rails sends form parameters in nested hashes.
{ page: { subject_id: 1, name: 'Hello World.' } }
So to whitelist the parameters you would do.
class PagesController < ApplicationController
def index
#test = Page.all
end
def new
#test = Page.new
end
def create
#test = Page.new(page_params)
if #test.save
flash[:notice] = "Page created successfully!"
redirect_to(:action => 'index')
else
render('new')
end
end
private
def page_parameters
params.require(:page)
.permit(:subject_id,:name,:position,:visible,:permalink)
end
end
which is like doing:
params[:page].slice(:subject_id,:name,:position,:visible,:permalink)
form_for and models
Also your form should read:
<%= form_for(:page, :url => {:action => 'create'}) do |f| %>
Or:
<%= form_for(#page, :url => {:action => 'create'}) do |f| %>
:Page will give you the wrong key in your parameters and prevent rails from binding the form to your #page object. (The values posted will disappear from the form when it's invalid).
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 %>
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