感谢阅读这篇文章.过去几天我一直坚持使用RoR的问题.我在index.html.erb下面有一个表单:
Ajax List Demo <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>Add to list using Ajax
<% form_tag :action => :list , :method=>:get, :remote=>true do %> Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %> <%= submit_tag "Find" %> <% end %>
在控制器中我有:
def list puts "here!!!!" reader = Reader.new @profiles = reader.processURL(params[:url]) #profileList = respond_to do |format| #format.html { render :partial=>true, :locals => { :profiles => @profiles}}#{ render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles}} format.js {render :content_type => 'text/javascript', :locals => { :profiles => @profiles}} # index.html.erb # format.rss render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles} end
并将远程UJS的js文件作为list.js.erb
$("#my_list").html("<%= escape_javascript(render(:partial => "list"))%>");
问题是我无法在div标签my_list中获得结果来呈现部分_list.html.erb.我得到一个空白页面,406错误.如果我在控制器中取消注释渲染html代码,我会在浏览器中渲染部分后退.我有点卡住,我想提交表单和结果弹出my_list div.我是新手,所以如果我错过了一些明显的东西,请毫不犹豫地向我指出......我绝对愿意尝试.
将其改为:
Ajax List Demo Listing posts
<%= javascript_include_tag 'jquery.js' %> <%= csrf_meta_tag %>Add to list using Ajax
<% form_tag :action => :doit , :method=>:get, :remote=>true do %> Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %> <%= submit_tag "Find" %> <% end %>
控制器:
def doit puts "here!!!!" reader = Reader.new @profiles = reader.processURL(params[:url]) respond_to do |format| # format.html {render :partial=>true, :locals => { :profiles => @profiles}}#{ render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles}} format.js #{render :content_type => 'text/javascript', :locals => { :profiles => @profiles}} # index.html.erb # format.rss render :partial=>'profiles/list',:layout => false, :locals => { :profiles => @profiles} end
JS _doit.js.erb $("#my_list").html("<%= escape_javascript(render(:partial =>"doit"))%>");
最后是部分:
_doit.html.erb.
但是我仍然得到406错误,我没有重复_doit js或erb.有什么突出的不正确吗?再次感谢!
另一个更新:
我认为表单没有正确呈现:
这呈现:
<% form_tag :action => :doit , :remote=>true, :id => 'myform' do %> Enter the public url:<%= text_field_tag 'url' ,'', :size => 80 %> <%= submit_tag "Find" %> <% end %>
这个: