0
我有一個應用程序,我想在後端做一些計算,然後通過Ajax在前端顯示結果。我沒有使用ActiveRecord。如何在Rails 3中使用js.erb模板和Ajax?
我顯示了JavaScript代碼而不是執行它。 Webrick在按下轉換按鈕後說:
Started POST "/site/convert.js" for <ip address> at 2011-04-05 01:37:09 +0300
Processing by SiteController#convert as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"K9De9/J5bsnOxqLdmvl02sOVgmU3c4gEZ/n7DBwJdrw=", "radix"=>"10", "number"=>"2", "commit"=>"Convert"}
Rendered site/convert.js.erb (0.5ms)
Completed 200 OK in 28ms (Views: 22.9ms)
爲什麼JavaScript代碼沒有執行?
site_controller.rb:
class SiteController < ApplicationController
def index
end
def convert
number = params[:number]
from = params[:radix]
@decimal = Converter::convert(number, from.to_i, 10)
@hexadecimal = Converter::convert(number, from.to_i, 16)
@binary = Converter::convert(number, from.to_i, 2)
end
end
網站/ index.html.erb:
<%= form_tag site_convert_path(:format => :js), :id => 'conversion', :remote => true do %>
<%= radio_button_tag 'radix', 10, true %> Decimal
<%= radio_button_tag 'radix', 16 %> Hexadecimal
<%= radio_button_tag 'radix', 2 %> Binary
<%= number_field_tag 'number', '', :size => '20', :min => 0 %>
<%= submit_tag 'Convert' %>
<% end %>
<button id="convert" type="button">Convert</button>
<table>
<thead>Conversions</thead>
<tr>
<th>Decimal</th>
<th>Hexadecimal</th>
<th>Binary</th>
</tr>
<tr>
<td id="decimal_converted"></td>
<td id="hexadecimal_converted"></td>
<td id="binary_converted"></td>
</tr>
</table>
網站/ convert.js.erb:
$('#decimal_converted').text("<%= @decimal %>");
$('#hexadecimal_converted').text("<%= @hexadecimal %>");
$('#binary_converted').text("<%= @binary %>");
的routes.rb:
Conversions::Application.routes.draw do
root :to => 'site#index'
post 'site/convert'
end