简体   繁体   English

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

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

I am stuck; 我被困住了; I did a search and I can't find a pointer on how to get this project to work. 我进行了搜索,但找不到如何使该项目正常工作的指针。 I have a form on my index.html.erb as: 我在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>

Now I have a div with this called my_list. 现在,我有了一个名为my_list的div。 When I submit the form I want my partial _list.html.erb (inside profiles dir, tried home dir too) to update with the results of list. 当我提交表单时,我希望我的部分_list.html.erb(在个人档案目录中,也尝试过主目录)更新为list的结果。

My home controller has this method: 我的家庭控制器具有以下方法:

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

I end up getting this error: 我最终收到此错误:

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)

Could anyone help me or send me in the right direction to understand how to update a div rendering via Rails on form submit? 谁能帮助我或向我发送正确的指导,以了解如何通过Forms Submit上的Rails更新div渲染? Thanks again. 再次感谢。

First of all I am getting ajax to render(@homepages) in index.html.erb via index.js.erb OK, but my search to update @homepages is not working. 首先,我通过index.js.erb将ajax render(@homepages)index.html.erb render(@homepages) ,但我对更新@homepages的搜索无法正常工作。

In your javascript_include_tag you missed out rails.js which I think handles the ajax request. 在您的javascript_include_tag您错过了我认为可以处理ajax请求的rails.js javascript_include_tag :defaults would have done all that for you plus some others that you do not need. javascript_include_tag :defaults会为您完成所有这些工作,再加上一些您不需要的其他工作。 Providing you are not using jquery you need prototype and rails.js as a minimum. 如果您不使用jquery,则至少需要prototype和rails.js

I do not think the format of this form_tag is correct: 我认为此form_tag的格式不正确:

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

The first value after the "(" should be the item_path , then the conditions :action => :list . “(”之后的第一个值应该是item_path ,然后是条件:action => :list

:method => post should take you to the create or new action. :method => post应该带您进行createnew操作。

:update => 'my_list' would be done in the list.js.erb using javascript (prototype) :update => 'my_list'将使用javascript(原型)在list.js.erb完成

The code for jquery and prototype is similar, but slightly different (see Railscast 205 ) jQuery和原型的代码相似,但是略有不同(请参见Railscast 205

Why format xml in the form_tag, put it in the controller "list" mine is: 为什么在form_tag中设置xml格式,然后将其放入控制器的“列表”中是:

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

Don't sure if you can get away with 'url' should not it be :url you might be able to use either. 不确定是否可以摆脱'url'的限制,否则不应该使用:url。

When I was getting the missing template it was because it was looking for a search.html.erb file. 当我得到缺少的模板时,是因为它正在寻找search.html.erb文件。 In your case it is looking for a list.html.erb . 在您的情况下,它正在寻找list.html.erb

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

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