我正在尝试使用ajax更新页面的特定部分,但js.erb文件似乎并没有替换我的div中的内容。我也注意到当地人因某种原因没有被传递给我的部分。在我的控制器中,我有一个加载show.html.erb的show动作,当单击一个项目时,它会发送一个ajax请求。这些项目位于具有id =" stream-contents"的div中。请求将发送到我的控制器中名为get_tree_folder的操作,响应格式为JS。 js.erb和部分文件与我的show.html.erb位于同一文件夹中。
这是我的控制器动作get_tree_foler:
def get_tree_folder
@file_view = false;
@curriculum = Curricula.find_by_id(params[:id])
@git = get_bare_repo @curriculum
@branch = params[:branch]
tree = @git.gtree(params[:tree])
@child_trees = tree.trees
@child_blobs = tree.blobs
respond_to do |format|
format.js
end
end
我的js.erb文件get_tree_folder.js.erb:
$('#stream-contents').html(<%= escape_javascript(render :partial => 'tree', :locals => { branch: @branch, child_trees: @child_trees, child_blobs: @child_blobs, curriculum: @curriculum }) %>);
控制台输出:
Started GET "/curricula/5/master/a9d280e6a0eb41ab7ea012f10f516913b2e53061" for 127.0.0.1 at 2014-03-27 13:52:36 -0700
Processing by CurriculaController#get_tree_folder as JS
Parameters: {"id"=>"5", "branch"=>"master", "tree"=>"a9d280e6a0eb41ab7ea012f10f516913b2e53061"}
Curricula Load (0.3ms) SELECT `curriculas`.* FROM `curriculas` WHERE `curriculas`.`id` = 5 LIMIT 1
Rendered curricula/_tree.html.erb (1.1ms)
Rendered curricula/get_tree_folder.js.erb (2.9ms)
Completed 200 OK in 17ms (Views: 5.9ms | ActiveRecord: 0.3ms | Solr: 0.0ms)
非常感谢任何建议。
答案 0 :(得分:1)
将参数包装在引号中的$('#stream-contents').html()
:
$('#stream-contents').html("<%= escape_javascript(render :partial => 'tree', :locals => { branch: @branch, child_trees: @child_trees, child_blobs: @child_blobs, curriculum: @curriculum }) %>");