Topic: respond_to in Rails 3

Hello all,

Is late and I'm been trying to figure out how I can make this code work in Rails 3
I want to make an Ajax call using Jquery and return a partial that then Jquery will place on my page.
I'm not using any Rails javascript helpers so I'm just using pure Jquery. This is what I got

            url: "<%= properties_path %>",
            type: "GET",
            success: function(data) {

  respond_to :html, :xml, :js
  def index
    @properties = Property.all
    respond_with (@properties) do |format|
      format.js{ render :partial => 'properties', :layout=>false }

Normally I would use (rails 2.x) respond_to do |format| but that doesn't work in rails anymore and I don't think the :js woks either as it currently responds to :json, :xml, :html but not :js
What this is doing is just rendering the full html page.

Any suggestions on how to render this partial with this Ajax call?

Last edited by firebait (2010-10-26 02:44:48)

Re: respond_to in Rails 3

Although I am not using jQuery I think the answer to my other posts which is printed below might help you:-

I have cracked it by going onto an IRC chat room ( RubyonRails) and a ProjectZen (human being somewhere out there in the ether) helped me to get it working.

Apparently what was happening was that I was following Ryan Bates who does many extremely good Railcast videos, but he builds on previous Railcast. Therefore in his 205 Railscast, which deals with Ajax calls, he did not mention that you must have:-

format.js in the action in the controller.

His xxxx.searchxxxxx needs to be created in the controller or model.

And that when I did :-

<%= render(@homepages)%>   (in his case <%= render(@products)%>)

The render was looking for a partial called "_homepage" (not "homepages") (I did not even have a partial therefore I got the UTF8 to ASCII error).

And then in "_homepage" I would add my code to render the results.

What I have now done in my index.html.erb is to put <%= render(@homepages)%> , in the (div id = testsearch) in place of the code I use to render @homepages and then place that code in a partial "_homepage". Now I can use "_homepage" for the html and the Ajax call.

At the moment I have a slight problem in that it is rendering all the data in the"@homepages" as many times as the number of records.
At the moment I do not know why, but at least the Ajax call is working.

Re: respond_to in Rails 3

Here is my full code if anyone is interested. This code Observes the text_field_tag "search" every 2 seconds and if there is a change in the value it triggers a search automatically. The submit button can now be done away with I think. I might add ":autocomplete => "off", :onKeyPress=>"return disableEnterKey(event)") %>" to the text_field_tag to disable the return key, not sure.

In my index.html.erb I have:-

    <h1>Listing homepages</h1>
    <div id = "testsearch">
       <%=render :partial => 'homepage'%>     
    <%= form_tag homepages_path, :method => 'get', :remote => true do %>
    <%= label_tag(:search, "Search for:") %>
    <%= text_field_tag :search, params[:search]%>
    <%= submit_tag "search", :name => nil %>

    <%= set_focus_to_id 'search' %>               // I have a helper "set_focus_to_id"

    document.observe("dom:loaded", function() {   // ensures the page is loaded first
    new Form.Element.Observer(                    // Observes the text_field_tag every 2 seconds
      respondToChange                         //refrences the function in the Layout <head>
    )                                         // on a change in search calls respondToChange
    <br />
    <%= link_to 'New Homepage', new_homepage_path %>

In my application Layout head I have:_

    function respondToChange() {
    $('search').up('form').submit()            // The ".up finds the form in the DOM"

In my controller#index I have:-

     def index
      @homepages =[:search]) //".search method is in the Model"
      respond_to do |format|
       format.html # index.html.erb
       format.xml  { render :xml => @homepages }

In my Model I have:-

       if search_item
        self.where('section LIKE ?', "%#{search_item}%")    //Handles the ajax call.
        self.all                                            //Handles the html call on startup.

In the helper I have:-

    def set_focus_to_id(id)

In the "_homepage" partial I have:-


    <% for homepage in @homepages %>

        <td><%= %></td>
        <td><%= homepage.section %></td>
        <td><%= %></td>
        <td><%= homepage.description %></td>
        <td><%= link_to 'Show', homepage %></td>
        <td><%= link_to 'Edit', edit_homepage_path(homepage) %></td>
        <td><%= link_to 'Destroy', homepage, :confirm => 'Are you sure?', :method => :delete %></td>


And in the index.js.erb I have:-

    $('testsearch').update("<%= escape_javascript(render :partial => 'homepage') %>");

If anyone has any comments on how I could improve this please contact me or say so.