0

I'm using haml so hopefully you can understand the html going on here. It's easier to read in my opinion but some people don't like it :p

I have a form on my edit page that looks like this:

.center_col
  -# I got into this weird habit of using "data-remote" for remote submission, w/e
  = form_for @content, :html => {"data-remote" => true, :id => "page_editor", "data-page-id" => @content.id} do |f|
    %h2
      = f.label :textile, "Text", :class => 'inline'
      (Parsed with
      = link_to("Textile", "http://redcloth.org/textile/", :class => 'external') + ")"
    %ul.fields
      %li.field
        = f.text_area :textile, :class => "full-field", "data-remote-preview" => preview_page_path(@content)
        = display_error(:content, :textile)
      %li.submit.left
        = f.submit "Save Changes", :class => "rm_lr_margin"

When you submit the form I want the controller action to be rendering the _page partial. Right now the controller action is attempting to render 'pages/_page.js.haml', but I want to use 'pages/_page.html.haml' because I have an after-success jquery hook that automatically replaces the content in the edited area of the page with the new text.

Here's my action:

def update
  load_content(params[:id])
  @content.textile = params[:page][:textile]

  if @content.save
    render :partial => "pages/page", :content_type => "text/html", :layout => false, :locals => {:page => @content}
  else
    render :action => 'edit', :layout => false
  end
end

Elsewhere...

def load_content(page_name)
  @content = Page.find_by_name!(page_name)
  @page = @content.name
end

So this is all working. Except it's rendering the wrong view :( When you first get to the page a section of it is populated using the 'pages/_page.html.haml' partial, and when you update the content with this ajax action I would like to use that same partial instead of having to duplicate the contents verbatim under the name 'pages/_page.js.haml'. Does anyone know what I'm missing here to make the action render haml content, not js content? When I perform the update right now it gives me this error:

Template is missing

Missing partial pages/page with {:formats=>[:js, "application/ecmascript", "application/x-ecmascript", "*/*"], :locale=>[:en, :en], :handlers=>[:rxml, :haml, :builder, :rjs, :rhtml, :erb]}

If I copy the html.haml file and name is .js.haml it works as expected.

This is the jquery I'm using to handle the events that rails.js is giving me when the form is submitted:

$('form#page_editor').live({
  "ajax:beforeSend": function(xhr, settings){
    $.facebox.loading();
  },
  "ajax:success": function(e, data, status, xhr){
    $.facebox.close();
    $('#page_' + $(this).attr('data-page-id')).replaceWith(data);
  },
  "ajax:error": function(e, xhr, status, error){
    $.facebox(xhr.responseText);
  }
});

Don't think that part was relevant but it shows you when in the JS I'm updating the content on the page. I suspect the problem is either in my action's render call, or the view's form_for call. Any help would be appreciated :)

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
nzifnab
  • 15,876
  • 3
  • 50
  • 65
  • As far as I know Rails will render the views based on the call. ie if the call is html it will by default render html file. In case of AJAX call it will be by default a js call. Can you try adding `:defaults => { :format => 'html' }` for your action in the routes.rb – ssri Mar 14 '11 at 05:55

1 Answers1

0

You can ask for a specific format:

render :partial => "pages/page.html"
Michaël Witrant
  • 7,525
  • 40
  • 44