以下是live demo
我正在尝试使用远程操作显示列表框,只是搜索参数,但是无效。
Clients_controller.rb
def new
@client.new
end
def search_city
@cities = City.where('id= ?',params[:city_id])
respond_to do |format|
format.js
end
end
search_city.js.erb
$("#test").html("<%= escape_javascript(render(:partial => "city_results"))%>");
模型
Client.rb
belongs_to :city
City.rb
belongs_to :state
has_many :clients
State.rb
has_many :cities
的routes.rb
resources :clients do
match 'search_city',:via =>[:get], :on => :collection
end
_form.erb
<%= link_to "Get list cities", {:action => "search_city"}, :remote => true, :class => "button-link" %>
<div id = "test">Here this text will be replaced</div>
_city_results.erb
<%= select_tag "example",options_for_select(@cities.collect { |p| [p.name,p.id] }) %>
这里完整的日志:
Started GET "/clients/new" for 127.0.0.1 at 2016-04-14 17:31:03 -0500
Processing by ClientsController#new as HTML
Completed 200 OK in 133ms (Views: 107.4ms | ActiveRecord: 1.0ms)
Rendered client_management/clients/new.html.erb within layouts/application (21.6ms)
Started GET "/clients/search_city?city_id=1" for 127.0.0.1 at 2016-04-14 16:43:55 -0500
Processing by ClientManagement::ClientsController#search_city as JS
Parameters: {"city_id"=>"1"}
Province Load (0.2ms) SELECT `states`.* FROM `states` WHERE `states`.`city_id` = 1
Rendered clients/partials/_city_results.erb (1.9ms)
Rendered clients/search_city.js.erb (2.9ms)
Completed 200 OK in 7ms (Views: 4.7ms | ActiveRecord: 0.4ms)
INFO
SELECT `states`.* FROM `states` WHERE `states`.`city_id` = 1
RESULT: 1 (exist value)
问题:
未显示带选项的select标记。 当我检查params是否被发送时,一切正常,但在视图中没有被替换。
需要安装宝石吗?
只使用简单的文本而不需要rails代码。
我试过了:
$('#province').html("<%= escape_javascript( render :partial => 'clients/partials/city_results' ) %>");
$('#province').show();
答案 0 :(得分:2)
for d in range(0, len_string):
new_array.extend(d)