[英]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
應該帶您進行create
或new
操作。
: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.