开发者

Ruby On Rails 3 submit form AJAX update results in div

开发者 https://www.devze.com 2023-01-21 05:20 出处:网络
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:

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:

<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. 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.

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? 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.

In your javascript_include_tag you missed out rails.js which I think handles the ajax request. javascript_include_tag :defaults would have done all that for you plus some others that you do not need. Providing you are not using jquery you need prototype and rails.js as a minimum.

I do not think the format of this form_tag is correct:

<% 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.

:method => post should take you to the create or new action.

:update => 'my_list' would be done in the list.js.erb using javascript (prototype)

The code for jquery and prototype is similar, but slightly different (see Railscast 205)

Why format xml in the form_tag, put it in the controller "list" mine is:

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.

When I was getting the missing template it was because it was looking for a search.html.erb file. In your case it is looking for a list.html.erb.

0

精彩评论

暂无评论...
验证码 换一张
取 消