使用 link_to 指定控制器操作渲染 Ajax 部分时出现问题

发布于 2024-11-16 01:31:29 字数 914 浏览 0 评论 0原文

我在“proposition”显示页面上有一个 div (id="content"),我想根据选择的 div 顶部的选项卡在其中呈现不同的部分。

我正在尝试使用以下 link_to 通过 AJAX 渲染部分内容 (substantive_content):

<%= link_to "Connected", :url => {:controller => "propositions", 
                           :action => "substantive_content"}, :remote => true %>


我有两个问题:

  1. 单击链接时,出现 GET 请求 被发送到 PropositionsController#show,以及 不是(我认为)“实质性的” content”操作。这只是刷新页面,不会远程执行任何操作。不会显示正确的部分。

  2. 我不知道如何渲染 部分进入“内容”div。


目前我在 PropositionsController 中有这个:

  def substantive_content
     respond_to do |format|
         format.js{ render :update do |rightbar|
                 rightbar.replace_html 'content', 'Hello World!'
         end}
     end
  end


...但我不知道从这里去哪里。我是否以正确的方式处理这件事?我对 Rails、JavaScript 等非常陌生。

任何有关这些问题的帮助都会很棒。谢谢!

I have a div (id="content") on a "proposition" show page into which I want to render different partials depending on which tab at the top of the div is selected.

I'm trying to render a partial (substantive_content) with AJAX using the following link_to:

<%= link_to "Connected", :url => {:controller => "propositions", 
                           :action => "substantive_content"}, :remote => true %>

I've got two problems:

  1. On clicking the link, a GET request
    is sent to
    PropositionsController#show, and
    not (I presume) to the "substantive
    content" action. This just refreshes the page and doesn't do anything remotely. The correct partial isn't displayed.

  2. I'm not sure how then to render a
    partial into the "content" div.

At the moment I have this in the PropositionsController:

  def substantive_content
     respond_to do |format|
         format.js{ render :update do |rightbar|
                 rightbar.replace_html 'content', 'Hello World!'
         end}
     end
  end

... but am not sure where to go from here. Am I even going about this in the right way? I'm very new to rails, javascript, etc.

Any help with either of these problems would be fantastic. Thanks!

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

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

发布评论

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

评论(1

地狱即天堂 2024-11-23 01:31:29

在您的routes.rb 文件中,您应该有一个substantive_content 操作的条目。它可能看起来像这样:

match "substantive_content" => "propositions#substantive_content", :as => substantive_content

然后在您看来,链接应该看起来像这样:

<%= link_to "Connected", substantive_content_path, :remote => true %>

当 js 请求到达 substantive_content 操作时,我将渲染一个 js.erb 文件。该操作看起来像这样:

def substantive_content
  respond_to do |format|               
    format.js
  end        
end 

然后在您的views/propositions 目录中创建一个substantive_content.js.erb 文件。在该文件中,您可以使用 jquery 更新内容 div:

$('#content').html("<%= render :partial => 'your_partial_name_here' %>");

在不了解您正在处理的实际应用程序的更多信息的情况下,很难说这是否是完成您想要的事情的最佳方式。我通常会尝试让我的应用程序尽可能保持“RESTful”,因此我不愿意创建不属于正常索引、新建、创建、显示等设置的操作。这并不是说在这种情况下没有必要,但这是值得考虑的事情。

In your routes.rb file you should have an entry for the substantive_content action. It might look something like this:

match "substantive_content" => "propositions#substantive_content", :as => substantive_content

Then in your view the link should look something like this:

<%= link_to "Connected", substantive_content_path, :remote => true %>

I would render a js.erb file when the js request comes to the substantive_content action. The action would look soemthing like this:

def substantive_content
  respond_to do |format|               
    format.js
  end        
end 

Then create a substantive_content.js.erb file in your views/propositions directory. In that file you can use jquery to update the content div:

$('#content').html("<%= render :partial => 'your_partial_name_here' %>");

Without knowing more about the actual application you're working on, its hard to say if this is the best way to accomplish what you want. I generally try to stay as "RESTful" as possible with my apps so I'm reluctant to create actions aren't part of the normal index, new, create, show, etc. set. That's not to say that it isn't neccesary in this case, but it's something to think about.

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