Ruby on Rails - 在.js.erb中修改嵌入式Ruby的一部分

时间:2022-11-07 08:47:42

I'm working on a single-page site and have gotten the ajax loading of templates to insert into the content part of the site working. However, I'm having trouble doing this with multiple templates, using a parameter.

我正在开发一个单页网站,并且已经将模板的ajax加载插入到网站的内容部分中。但是,使用参数我无法使用多个模板执行此操作。

I have 5 templates, shared/blog, shared/projects, etc.

我有5个模板,共享/博客,共享/项目等。

In my controller, I'm doing an AJAX call to 'replace'

在我的控制器中,我正在进行'替换'的AJAX调用

pages = ['blog', 'projects', 'resume', 'gallery', 'contact']

def replace
  @content = params[:content]
  if not pages.include? content
    content = 'blog'
  end
  respond_to do |format|
    format.js
  end
end

In replace.js.erb, I have this code:

在replace.js.erb中,我有这个代码:

$(".content_inner").html("<%= j render(:partial => 'shared/blog') %>");

I have kept it just saying 'shared/blog' because it works for loading the blog if I keep the embedded Ruby static like that. However, I can't figure out how to replace the 'blog' part of 'shared/blog' in here to whatever is in the @content variable. I've tried things like #{content}, but to no avail.

我保持它只是说'共享/博客',因为如果我保持嵌入式Ruby静态,它可以加载博客。但是,我无法弄清楚如何将此处的'shared / blog'的'blog'部分替换为@content变量中的任何内容。我尝试过像#{content}这样的东西,但无济于事。

(It does receive the content variable correctly, the issue is just with using it)

(它确实正确接收内容变量,问题只是使用它)

Any help would be appreciated!

任何帮助,将不胜感激!

Thanks.

1 个解决方案

#1


0  

String interpolation requires double quotes. You're after:

字符串插值需要双引号。你是在追求:

$(".content_inner").html("<%= j render("shared/#{@content}") %>");

A few notes:

几点说明:

  • The :partial => hasn't been necessary for years in Rails. Just use render <partial_name>.
  • Rails中多年来一直没有:partial =>。只需使用render

  • Rails already comes with a place to store your shared partials: app/views/application. You should move your shared partials there, and then you can render them simply by using render(@content). This is important to how Rails works, because it allows you to override the partial in controller-specific view paths. For example, calling render("blog") will render app/views/<controller_name>/blog.js.erb if it exists, and then fallback to app/views/application/blog.js.erb otherwise.
  • Rails已经有了一个存储共享部分的地方:app / views / application。您应该在那里移动共享的部分,然后您可以使用render(@content)简单地渲染它们。这对Rails如何工作很重要,因为它允许您覆盖控制器特定视图路径中的部分。例如,调用render(“blog”)将呈现app / views / /blog.js.erb(如果存在),然后回退到app / views / application / blog.js.erb。

#1


0  

String interpolation requires double quotes. You're after:

字符串插值需要双引号。你是在追求:

$(".content_inner").html("<%= j render("shared/#{@content}") %>");

A few notes:

几点说明:

  • The :partial => hasn't been necessary for years in Rails. Just use render <partial_name>.
  • Rails中多年来一直没有:partial =>。只需使用render

  • Rails already comes with a place to store your shared partials: app/views/application. You should move your shared partials there, and then you can render them simply by using render(@content). This is important to how Rails works, because it allows you to override the partial in controller-specific view paths. For example, calling render("blog") will render app/views/<controller_name>/blog.js.erb if it exists, and then fallback to app/views/application/blog.js.erb otherwise.
  • Rails已经有了一个存储共享部分的地方:app / views / application。您应该在那里移动共享的部分,然后您可以使用render(@content)简单地渲染它们。这对Rails如何工作很重要,因为它允许您覆盖控制器特定视图路径中的部分。例如,调用render(“blog”)将呈现app / views / /blog.js.erb(如果存在),然后回退到app / views / application / blog.js.erb。