Rails在同一页面提交后显示信息

时间:2015-06-03 15:31:57

标签: javascript ruby-on-rails ruby ajax forms

我希望在单击表单提交后显示一些值,但是在同一页面中并且不刷新。

以我的形式

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

和我的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') %>");

并在我的控制器中

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

但是当我点击提交表单时,没有任何反应。

控制台信息。

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)

这是我第一次问,谢谢!!

1 个答案:

答案 0 :(得分:1)

当您发送AJAX请求时,Ruby会自动查找同名的public static string StrToHex(string strMessage) { byte[] byteArray = Encoding.UTF8.GetBytes(strMessage); string strHex = BitConverter.ToString(byteArray); strHex = strHex.Replace("-", ""); return strHex; } 。因此,您可以将.js.erb重命名为create.js.erb并删除search.js.erb块。另外,respond_to可能导致响应被解释为JSON而不是JS,因此它不会被执行。