Reputation: 135
i want to show some values after click in form submit but in the same page and without refresh.
in my form
<%= simple_form_for :search, :url => url_for(:controller => "reports", :action => "search"),
:method => "get", :remote => true,:format => :js,
html: {id: "date_range", :"data-type" => "json"} do |f| %>
<%= f.input :start_date, wrapper: :ranged_datetime,input_html: {id: "datetimepicker6"}, :label => "Fecha Inicio" %>
<%= f.input :end_date, wrapper: :ranged_datetime, input_html: {id: "datetimepicker7"} , :label => "Fecha Final" %>
<%= f.submit 'Buscar', :class => "btn btn-success btn-lg" %>
<% end %>
<div class="datos">
<%= render :partial => 'reports/shared/sale_by_date' %>
</div>
and in my sale_by_date.html.erb
<% unless @sales.nil? %>
<% @sales.each do |sale| %>
<tr class="<%= sale.remaining_balance == 0.00 && !sale.total_amount.blank? ? 'success' : '' %>">
<td><%= sale.sale_by_vet %></td>
<td><%= sale.created_at.strftime("%m/%d/%y %I:%M:%S %p") %></td>
<td><%= sale.customer.blank? ? '-' : sale.customer.last_name %></td>
<td><%= number_to_currency sale.total_amount %></td>
<td><%= number_to_currency sale.tax %></td>
<td><%= number_to_currency sale.paid_total %></td>
<td><%= sale.remaining_balance == 0.00 && sale.payments.count > 0 ? "Pagado" : "#{number_to_currency sale.remaining_balance}" %></td>
<td><%= link_to 'Mostrar', edit_sale_path(sale) %> | <%= link_to 'Eliminar', sale, method: :delete, data: { confirm: '¿Esta seguro?' } %></td>
</tr>
<% end %>
create.js.erb
$('.datos').html("<%= escape_javascript render(:file => 'reports/shared/_sale_by_date.html.erb') %>");
and in my controller
def search
start_date = params[:search][:start_date]
end_date = params[:search][:end_date]
@sales = Sale.where(:created_at => start_date..end_date)
respond_to do |format|
format.js { ajax_refresh }
end
end
def ajax_refresh
return render(:file => 'reports/create.js.erb')
end
but when i click in submit form, nothing happens.
console info.
Started GET "/reports/search? utf8=%E2%9C%93&search%5Bstart_date%5D=01%2F01%2F2015&search%5Bend_date%5D=06%2F03% 2F2015&commit=Buscar" for ::1 at 2015-06-03 10:00:44 -0500
Started GET "/reports/search?utf8=%E2%9C%93&search%5Bstart_date%5D=01%2F01%2F2015&search%5Bend_date%5D=06%2F03%2F2015&commit=Buscar" for ::1 at 2015-06-03 10:00:44 -0500
Processing by ReportsController#search as JSON
Processing by ReportsController#search as JSON
Parameters: {"utf8"=>"✓", "search"=>{"start_date"=>"01/01/2015", "end_date"=>"06/03/2015"}, "commit"=>"Buscar"}
Parameters: {"utf8"=>"✓", "search"=>{"start_date"=>"01/01/2015", "end_date"=>"06/03/2015"}, "commit"=>"Buscar"}
[query values]
Rendered reports/shared/_sale_by_date.html.erb (65.5ms)
Rendered reports/shared/_sale_by_date.html.erb (65.5ms)
Rendered reports/create.js.erb (67.5ms)
Rendered reports/create.js.erb (67.5ms)
Completed 200 OK in 80ms (Views: 56.8ms | ActiveRecord: 16.0ms)
Completed 200 OK in 80ms (Views: 56.8ms | ActiveRecord: 16.0ms)
it's my first time asking, thanks !!
Upvotes: 1
Views: 1615
Reputation: 616
Ruby will automatically look for a .js.erb
of the same name when you send an AJAX request. So you can rename your create.js.erb
to search.js.erb
and remove your respond_to
block. Additionally the :"data-type" => "json"
may be causing the response to be interpreted as JSON instead of JS, and as a result it is not executed.
Upvotes: 1