I'm building a rails app With Rails 4 Ruby 2
Background:
In my show view i have a nested element displaying, in this element I have a custom button to update a record. (displayed below), i have created and if/else statement to either show the button, or if the button has been clicked, show the time it was clicked at.
Problem:
The problem I have encountered is that this works fine when only one line item is present. When I add several line items, and hit the button on the top line it removes all the buttons and shows the 00:00:00 time on the remaining lines.
I would like it so that when I push the button on Line item one, the button then disappears only for that line item, leaving the rest to be clicked.
My View Page:
<table class="table" id="resp-tbl">
<thead>
<tr>
<td><center>UNIT</center></td>
<td><center>STATUS</center></td>
<td><center>DISPATCHED </center></td>
<td><center>RESPONDING </center></td>
<td><center>ON SCENE </center></td>
<td><center>CLEAR AT</center></td>
<td></td>
</tr>
</thead>
<tbody>
<% #respondings.each do |responding| %>
<tr>
<td><center><%= responding.user.employee_ident %></center></td>
<td><center><%= responding.status %></center></td>
<td><center><%= responding.created_at.strftime("%H:%M:%S") %></center></td>
<td><center>
<% if #respondings_true.present? %>
<%= responding.responding_tme.strftime("%H:%M:%S") %>
<% else %>
<%= link_to "Responding", unit_responding_update_call_responding_path(call_id: #call.id, id: responding.id), method: :patch, class: 'btn btn-nav btn-primary', :id => 'respond-btn' %>
<% end %>
<td><center><%= responding.on_scene_tme.strftime("%H:%M:%S") %></center></td>
<td><center><%= responding.clear_tme.strftime("%H:%M:%S") %></center></td>
<td><center><%= link_to "Remove Unit", [#call, responding], class: 'btn btn-danger btn-sm', method: :delete, confirm: "Are you sure you want to remove this unit?" %></center></td>
<td></td>
</tr>
<% end %>
</tbody>
</table>
My Custom Controller Action:
#Custom Controller Calls
def unit_responding_update
#call = Call.find(params[:call_id])
#responding = Responding.find(params[:id])
#responding.responding_tme = DateTime.now
#responding.responding = "true"
#responding.on_scene = "false"
#responding.clear = "false"
#responding.status = "RESPONDING"
#responding.save!
respond_to do |format|
if #responding.save
format.html { redirect_to #call, notice: "Responding time successfully updated." }
else
format.html { render action: 'edit' }
end
end
end
The filter should be reading the update action to status here and only removing the button and replacing it with the time if the responding.status = RESPONDING
I added this to my show method in my calls controller.
#respondings_true = #call.respondings.select{ |x| x.status == 'RESPONDING'}
Like I said above, The action should originally display the button because until the update button is pushed, the status is blank.
Thanks in advance for your assistance here.
<% if #respondings_true.present? %>
#respondings_true = #call.respondings.select{ |x| x.status == 'RESPONDING'}
Well, the issue is that once you update one line item, #respondings_true would always be true.
the if statement should base on the line item, you can change the if statement to something like
<% if responding.status == 'RESPONDING' %>
Related
I want to store value in db using Rails ajax form but it is not storing anything.Please let me to know where i did the mistake and help me to resolve this issue.
I am explaining my code below.
users/index.html.ebr:
<script type="text/javascript">
$('form').submit(function() {
var valuesToSubmit = $(this).serialize();
console.log('hello')
$.ajax({
type: "POST",
url: $(this).attr('create'), //sumbits it to the given url of the form
data: valuesToSubmit,
dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
}).success(function(json){
console.log("success", json);
});
return false; // prevents normal behaviour
});
</script>
<p><%= flash[:notice] %></p>
<%= form_for :user do |f| %>
<p>
Name : <%= f.text_field :name,placeholder:"Enter your name" %>
</p>
<p>
Email : <%= f.email_field :email,placeholder:"Enter your email" %>
</p>
<p>
Content : <%= f.text_field :content,placeholder:"Enter your content" %>
</p>
<p>
<%= f.text_field :submit,:onchange => "this.form.submit();" %>
</p>
<% end %>
<div id="sdf-puri" style="display:none" >
</div>
controller/users_controller.rb
class UsersController < ApplicationController
def index
#user=User.new
respond_to do |format|
format.html
format.js
end
end
def create
#user=User.new(params[:user])
if #user.save
flash[:notice]="user created"
end
end
end
create.js.erb
$("#sdf-puri").css("display", "block");
$("#sdf-puri").html("<%= escape_javascript (render 'table' ) %>");
$("#sdf-puri").slideDown(350);
create.html.erb
<%= render 'table' %>
_table.html.erb
<table>
<tr>
<th>Name :</th>
<th>Email :</th>
<th>Content :</th>
</tr>
<% #user.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.content %></td>
</tr>
<% end %>
</table>
After submit value should display in same index page.Please help me.
Rails has a built in Ajax system, on your form you need to add remote: true
form_for(#user, remote: true) do |f|
This tells rails to submit the form using Ajax under the hood... You don't need all of the script above the form
Your controller already correctly has the line:
responds_to js
So all you would do is have a create.js file in your views/user folder that handled your return Ajax stuff such as appending or fading etc
you need to add remote: true in form tag
I'm trying to make a sort part to my ruby erb list, so far it makes the list then i want a way to sort the list when the click "sort by date". I have no idea where to start?
I'm sure whether to use the href tag or what to use.
<table class="user_display">
<tr>
<th>Name</th>
<th>Date</th>
<th>Friends</th>
</tr>
<% #mentions.each do |name| %>
<tr>
<td><%=h name %></td>
<td><%=h date %></td>
<td><%=h friends %></td>
</tr>
<% end %>
</table>
this isn't the real code but it follows the same ideas with some names changed.
so How do I have a button or link that when they click it reorders the array?
i feel like i should pass something along the lines of
<input class="order" type="button" name="date" value="<%=h params[:orderdate] %>"/>
is this the right idea, if so how do I link to controller file? which has
get '/list' do
#mentions = array_of_names
erb :list
end
any ideas?
Try to add this to the view:
<%= link_to 'Order by date', sort_mentions_path(:order_type => 'date'), :class => 'btn' %>
<%= link_to 'Order by name', sort_mentions_path(:order_type => 'name'), :class => 'btn' %>
In the controller could be something like this:
def sort
if( params[:order_type] == 'name')
#mentions = Mention.order('name')
elsif ( params[:order_type] == 'date')
#mentions = Mention.order('date')
else
#mentions = Mention.all
end
respond_to do |format|
format.html { }
format.json { render json: #mentions }
end
end (Remember to add sort.html.erb with the list view)
And the route:
resources :mentions do
collection do
get 'sort'
end
end
This will put in #mentions the array ordered by date if you have the Object 'Mention':
#mentions = Mention.order(:date)
You don't need to pass any parameter from the view, you can manage the order in the controller
I want to display the database values inside the table using Ajax in Rails.bou i got the following error.
Error:
Template is missing
Missing template users/search, application/search with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "c:/Site/demo2/app/views"
I also added the search.html.erb i removed the error but got the blank page.
Please check my below codes.
views/users/index.html.erb
<%= form_for :user ,:url => {:action => "search" }, remote: true do |f| %>
<div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon text-left">Receipt No. Scan :</span>
<%= f.text_field :receipt,:class => "form-control",placeholder:"user number",:onchange => "this.form.submit();" %>
</div>
<% end %>
<div id="search-output-table">
<%= render partial: "search_output_table", locales: {user: #user} %>
</div>
controller/users_controller.rb
class UsersController < ApplicationController
def index
#user=User.new
end
def search
#user = User.find_by_receipt(params[:user][:receipt])
end
end
views/users/_search.js.erb
$("#search-output-table").html("<%= escape_javascript( render(partial: "search_output_table") ) %>");
views/users/_search_output_table.html.erb
<table>
<tr>
<th>User Name</th>
<th>User Email</th>
<th>User Number</th>
</tr>
<tr>
<td><%= #user.name %></td>
<td><%= #user.email %></td>
<td><%= #user.receipt %></td>
</tr>
</table>
I also want remove onchange event and as soon as the value will fill inside text field the action will execute without reload the page as well as the table value will display(initially the table should remain disable/hide).Please help me to resolve this error and add this new scenario.
Well, on request to UsersController#search Rails will by default render app/views/users/search.html.erb, which I assume is empty.
You probably just need to rename views/users/_search.js.erb to views/users/search.js.erb and add following to the controller
def search
#user = User.find_by_receipt(params[:user][:receipt])
respond_to do |format|
format.js
end
end
Also make sure you're passing in #user to the search_output_table template. Like this
render partial: 'search_output_table', locals: {user: #user}
And in 'search_output_table' you should use just user instead of #user.
I have successfully tried ajax saving in my sample formtastic with ajax form.
The new value is added to the database. But the the problem is in retrieving the list from the database as soon as i save via ajax.
How to do it.?
As soon as I add a new record I want my displaying list to be update. Both the option to add new record and list the data from database is in same page
This is my Index page. The controller and all other created via scaffolding
<h1>Listing samples</h1>
<table>
<tr>
<th><%=t :Name%></th>
<th></th>
<th></th>
<th></th>
</tr>
<% #samples.each do |sample| %>
<tr>
<td><%= sample.name %></td>
<td><%= link_to 'Show', sample %></td>
<td><%= link_to 'Edit', edit_sample_path(sample) %></td>
<td><%= link_to 'Destroy', sample, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Sample', new_sample_path %>
<br /><br /><br /><br /><br />
<%= semantic_form_for #sample1,:url => samples_path, :remote => true do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
When you save via ajax, you may have to change the way your controller responds, kinda like this:
def create
# stuff
respond_to do |format|
format.js
end
end
When you do this, rails would expect you to have created a file named create.js.erb, in which you can manipulate the data of your view(append new content to your table, render a whole new partial with your new object list, etc):
$('#your_list').html(
"<%= escape_javascript(render('your_table_partial')) %>"
);
Or
$('#your_list').append(
"<%= escape_javascript(render('your_item_of_the_table_partial')) %>"
);
These are just examples, i don't know your code enough to write the correct code for you, but you sure can use these as a base for your work.
I did the CRUD operation using these guidelines and it worked perfect
http://stjhimy.com/posts/07-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript
I implemented destroy functionality using rjs template in rails. i got an error "XML Parsing Error: no element found" when destroy one record from database. is this right my coding?
I used the following versions ruby and rails:
Ruby version: 1.8.7
Rails Version: 2.3.8
Controller file:
def destroy
begin
#task = Task.find(params[:id])
#task.destroy
respond_to do |format|
#format.html { redirect_to(tasks_url) }
format.js
format.xml { head :ok }
end
rescue Exception => e
puts e
end
end
partial template file _task.html.erb:
<tr id="<%= dom_id(task) %>">
<td><%= task.name %></td>
<td><%= task.note %></td>
<td><%= task.priority %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td>
<%= link_to 'Destroy', task, :confirm => 'Are you sure?', :method =>:delete,:remote => :true %>
index.rhtml file:
<div id='newform'>
<% form_for([#task, Task.new]) do |f| %>
<div>
<%= f.label 'Add a new task: ' %>
<%= f.text_field :name %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
</div>
<table id="alltasks">
<tr id="tablehead">
<th>Name</th>
<th>Note</th>
<th>Priority</th>
<th></th>
<th></th>
<th></th>
</tr>
<%= render #tasks %>
<!-- expanded: render :partial => "task", :collection => #tasks -->
</table>
<br />
<%= link_to 'New Task', new_task_path %>
destroy.js.rjs file:
page.alert('Hi')
The problem is that the xml format is getting requested from your destroy action, not the js format, and something is trying to parse the empty xml response produced by the format.xml { head :ok } line in the controller. It used to be that you could get away with the empty xml response, but it also looks like you're using unobtrusive javascript, and something somewhere in ujs tries to parse it.
One potential solution is to make a destroy.xml.builder view that produces a simple response, and change your controller action to read format.xml { render :layout => false } or something similar. You can also do some fun things with changing the data type your link is requesting, but I'd recommend avoiding that unless you really want to do something with your js response in destroy.js.rjs.