Hi I have been stuck on the following issue for days, and I just dont know whats going wrong...
I have the following form_tag code in my partial view, which is displayed with a remote add to cart from a main view:
<tr>
<% form_tag line_item, :method => :put do %>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
<td>
<%= text_field :line_item, :quantity, :size => 1, :value => line_item.quantity %>
<%= submit_tag 'update' %>
</td>
<% end %>
<td>
<%= button_to 'Delete Line Item', line_item, :method => :delete, :remote => true %>
</td>
</tr>
now when i click add to cart, the above is rendered in the id where I have the replace.html function targeted but...
i can't actually update the cart using the update button unless I refresh the page!! I have no idea what the matter is and its driving me mad!
Has anyone got any ideas what I may be doing wrong?
I think form_tag needs :remote => true also.
Related
I would like to get all records with their usernames when signed in as Admin. For that my controller code is as below;
amol361s_controller.rb
def index
if current_user.admin
#amol361s = Amol361.all.search(params[:search]).order("created_at ASC")
#users = User.all.amol361s
else
#amol361s = current_user.amol361s.all.search(params[:search]).visible.order("created_at ASC")
end
respond_to do |format|
format.html
format.js
end
end
index.html.erb
<tbody id = "kola">
<% #users.each do |user| %>
<%= user.amol361s.each do |amol361| %>
<tr class="tr-<%= cycle('odd', 'even') %>" id='<%= "tr_#{amol361.id}" %>'>
<% if current_user && current_user.admin %>
<td class="col-1"><%= link_to amol361.date.try(:strftime,'%d/%m/%Y'), edit_amol361_path(amol361) %></td>
<% else %>
<td class="col-1"><%= amol361.date.try(:strftime,'%d/%m/%Y') %></td>
<% end %>
<td class="col-3"><%= span_with_possibly_red_color amol361.description %></td>
<td class="col-1"><%= number_with_precision(amol361.amount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(amol361.discount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(amol361.paid, :delimiter => ",", :precision => 2) %></td>
<% #balance += amol361.amount.to_f - amol361.discount.to_f - amol361.paid.to_f %>
<% color = #balance >= 0 ? "pos" : "neg" %>
<td class="col-1 <%= color %>"><%= number_with_precision(#balance.abs, :delimiter => ",", :precision => 0) %></td>
<td class="col-1"><%= amol361.delndel %></td>
<td class="col-1"><%= amol361.remark %></td>
<% if current_user && current_user.admin %>
<td class="col-1"><%= link_to "Hide", hide_amol361_path(amol361), method: :put, remote: true, style: "color:#bb7272;" %>
<%= link_to amol361, method: :delete, data: { confirm: "Are you sure?" }, :class => 'delete_item' do %>
<i class="fa fa-trash" ></i>
<% end %>
</td>
<% end %>
</tr>
<% end %>
<% end %>
</tbody>
Right now I am getting the error as undefined method amol361s' for # User::ActiveRecord_Relation:0x00007f5dec19d088.
So, I am trying to get the records followed by usernames, similar to how we gather records based on month.
Any suggestions are most welcome.
Thank you in advance.
From what I understand, you can simply eager load user records if the current user is admin, like this:
#amol361s = Amol361.includes(:user).search(params[:search]).order("created_at ASC")
If you do this, it will be effective to fetch the user data simply by calling association on each member of this relation, like this:
# example:
#amol361s.first.user.username # or whatever the name of username column is
I'm not sure I understood your question correctly though, cause it's a little bit unclear to me.
I'm trying to create a simple page that will do simple calculations and return them on the same page. Ultimately i'll learn to use javascript and/or ajax to call them without reloading the entire page, but I'm new to programming so i'm trying to take incremental steps to get there.
I have been able to create the form and submit it with no errors. The result returns '0' and I think it's because when the page gets submitted and returns the same 'index' page it clears the instance variable. I have a few question:
How can I get the variable #result to actually return the result (and is this a good way of going about it?) and
Is it possible to load the result in the Vdrop number field?
In the future I want to be able to dynamically add rows (that include each variable, amps, volts, etc.) which would mean I would want to run the calculation more than once. I would assume I need to use some sort of "#variable do |f| 1..n" type of formula correct?
Thanks for help in advance!
Here's my controller code:
class CalculatorController < ApplicationController
def calculate
amps = params[:amps].to_i
volts = params[:volts].to_i
distance = params[:distance].to_i
phase = params[:phase].to_i
#result = amps + volts + distance + phase
render 'index'
end
end
Here's my index.html.erb code for the calculator page:
<div class="calculator index">
<h2>Calculator Test</h2>
<div>
<table class="table table-condensed">
<tr>
<th>Amps</th>
<th>Volts</th>
<th>Phase</th>
<th>Distance</th>
<th>VDrop</th>
</tr>
<%= form_tag({:controller => "calculator", :action => "calculate"}, :method => "post") do %>
<tr>
<td><%= number_field 'Amps' , :class => 'text_field', :step => 'any' %></td>
<td><%= number_field 'Volts', :class => 'text_field', :step => 'any' %></td>
<td><%= number_field 'Phase', :class => 'text_field', :step => 'any' %></td>
<td><%= number_field 'Distance', :class => 'text_field', :step => 'any' %></td>
<td><%= number_field 'Vdrop', :class => 'text_field', :step => 'any' %></td>
</tr>
</table>
<%= submit_tag('Submit')%>
</div>
<div>
<%= #result %>
</div>
<% end %>
</div>
The problem here is you're trying to pass instance variables, which are instances of that action to other actions cause technically the index.html.erb template is being rendered due to the index action which you don't have. What I'd do is:
class CalculatorController < ApplicationController
def index
if params[:amps]
amps = params[:amps].to_i
volts = params[:volts].to_i
distance = params[:distance].to_i
phase = params[:phase].to_i
#result = amps + volts + distance + phase
else
#result = "make a calculation"
end
end
end
Your form:
<%= form_tag ({controller: 'calculators', action: 'index'}), method: :get do %>
<td><%= number_field_tag :amps , nil %></td>
<td><%= number_field_tag :volts, nil %></td>
<td><%= number_field_tag :phase, nil %></td>
<td><%= number_field_tag :distance, nil %></td>
<td><%= number_field_tag :vdrop, nil %></td>
<%= submit_tag('Submit')%>
<% end %>
<%= #result %>
Will work. Just tried it.
Number of things to make it work: 1. it's number_field_tag not number_field. 2. try not to use "AMPS" for the params instead use :amps, the if params[:amps] wasn't detecting the "AMPS" unless i did params["AMPS"]
Controller is the same.
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.
This is my code
<td> <%= select_tag "options", options_for_select( [["Show",
"/registrars/"+registrar.id.to_s],["Edit", "/edit_registrars/"+registrar.id.to_s],
["Dashboard", "/homes/"+registrar.enrollment_application.id.to_s], **
[image_tag("file_cabinet.png"), "/registrars/"+registrar.id.to_s]**, ] ) %></td>
I want to use this
<%= link_to(image_tag("file_cabinet.png", :border=>0), :action => :show, :id =>
registrar, :filecabinet => true) %>
In above code if you see I tried but it is not working , any ideas??
You can't nest an image tag inside an option tag.