繁体   English   中英

Ruby On Rails 3在div中提交表单AJAX更新结果

[英]Ruby On Rails 3 submit form AJAX update results in div

我被困住了; 我进行了搜索,但找不到如何使该项目正常工作的指针。 我在index.html.erb上有一个表单,如下所示:

<html>   <head>
    <title>Ajax List Demo</title>   <h1>Listing posts</h1> 

    <%= javascript_include_tag "prototype" %>   </head>   <body>
    <h3>Add to list using Ajax</h3>
    <% form_tag (  :remote=>true, :update => 'my_list', :action => :list , :method=>:post, :format =>:xml ) do %>

      Enter the url:
      <%= text_field_tag 'url' ,'', :size => 80 %>
      <%= submit_tag "Find" %>
    <% end %>
    <div id="my_list"><%= render :partial => 'profiles/list' %></div>   </body> </html>

现在,我有了一个名为my_list的div。 当我提交表单时,我希望我的部分_list.html.erb(在个人档案目录中,也尝试过主目录)更新为list的结果。

我的家庭控制器具有以下方法:

def list
    puts "here!!!!"
   reader = Reader.new
  @profiles =  reader.processURL(params[:url])
    render :partial=>'profiles/list', :locals => { :profiles => @profiles}
end

我最终收到此错误:

ActionView::MissingTemplate (Missing partial profiles/list with {:locale=>[:en, :en], :handlers=>[:rjs, :builder, :rhtml, :rxml, :
erb], :formats=>[:xml]} in view paths "C:/Users/....tree/app/views"):
  app/controllers/home_controller.rb:22:in `list'

Rendered C:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/missing_template.erb
within rescues/layout (1.0ms)

谁能帮助我或向我发送正确的指导,以了解如何通过Forms Submit上的Rails更新div渲染? 再次感谢。

首先,我通过index.js.erb将ajax render(@homepages)index.html.erb render(@homepages) ,但我对更新@homepages的搜索无法正常工作。

在您的javascript_include_tag您错过了我认为可以处理ajax请求的rails.js javascript_include_tag :defaults会为您完成所有这些工作,再加上一些您不需要的其他工作。 如果您不使用jquery,则至少需要prototype和rails.js

我认为此form_tag的格式不正确:

<% form_tag (:remote=>true, :update => 'my_list', :action => :list , :method=>:post, :format =>:xml) do %>

“(”之后的第一个值应该是item_path ,然后是条件:action => :list

:method => post应该带您进行createnew操作。

:update => 'my_list'将使用javascript(原型)在list.js.erb完成

jQuery和原型的代码相似,但是略有不同(请参见Railscast 205

为什么在form_tag中设置xml格式,然后将其放入控制器的“列表”中是:

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @homepages }
  format.js
end

不确定是否可以摆脱'url'的限制,否则不应该使用:url。

当我得到缺少的模板时,是因为它正在寻找search.html.erb文件。 在您的情况下,它正在寻找list.html.erb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM