我的远程表单提交试图呈现pages/_page.js.haml文件,但我希望它是pages/_page.html.haml

发布于 2024-10-21 13:35:09 字数 2434 浏览 1 评论 0原文

我正在使用 haml,所以希望你能理解这里的 html。在我看来,它更容易阅读,但有些人不喜欢它:p

我的编辑页面上有一个表单,如下所示:

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

当您提交表单时,我希望控制器操作呈现 _page 部分。现在控制器操作正在尝试渲染“pages/_page.js.haml”,但我想使用“pages/_page.html.haml”,因为我有一个成功后的 jquery 挂钩,它会自动替换编辑后的内容包含新文本的页面区域。

这是我的行动:

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

在其他地方......

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

所以这一切都有效。除了它呈现错误的视图:(当您第一次访问页面时,它的一部分是使用“pages/_page.html.haml”部分填充的,当您使用此ajax操作更新内容时,我想使用它相同的部分,而不必在名称“pages/_page.js.haml”下逐字复制内容。有谁知道我在这里缺少什么来使操作渲染 haml 内容,而不是 js 内容?现在它给了我这个错误:

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]}

如果我复制 html.haml 文件并且名称是 .js.haml,它会按预期工作。

这是我用来处理表单时 Rails.js 给我的事件的 jquery。已提交:

$('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);
  }
});

不认为该部分相关,但它向您显示了我在 JS 中更新页面上的内容时的情况,我怀疑问题出在我的操作的 render 调用中,或者出现在视图的调用中。 form_for 呼叫。

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-10-28 13:35:09

您可以询问具体格式:

render :partial => "pages/page.html"

You can ask for a specific format:

render :partial => "pages/page.html"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文