使用 link_to 指定控制器操作渲染 Ajax 部分时出现问题
我在“proposition”显示页面上有一个 div (id="content"),我想根据选择的 div 顶部的选项卡在其中呈现不同的部分。
我正在尝试使用以下 link_to 通过 AJAX 渲染部分内容 (substantive_content):
<%= link_to "Connected", :url => {:controller => "propositions",
:action => "substantive_content"}, :remote => true %>
我有两个问题:
单击链接时,出现 GET 请求 被发送到
PropositionsController#show
,以及 不是(我认为)“实质性的” content”操作。这只是刷新页面,不会远程执行任何操作。不会显示正确的部分。我不知道如何渲染 部分进入“内容”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:
On clicking the link, a GET request
is sent toPropositionsController#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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的routes.rb 文件中,您应该有一个substantive_content 操作的条目。它可能看起来像这样:
然后在您看来,链接应该看起来像这样:
当 js 请求到达 substantive_content 操作时,我将渲染一个 js.erb 文件。该操作看起来像这样:
然后在您的views/propositions 目录中创建一个substantive_content.js.erb 文件。在该文件中,您可以使用 jquery 更新内容 div:
在不了解您正在处理的实际应用程序的更多信息的情况下,很难说这是否是完成您想要的事情的最佳方式。我通常会尝试让我的应用程序尽可能保持“RESTful”,因此我不愿意创建不属于正常索引、新建、创建、显示等设置的操作。这并不是说在这种情况下没有必要,但这是值得考虑的事情。
In your routes.rb file you should have an entry for the substantive_content action. It might look something like this:
Then in your view the link should look something like this:
I would render a js.erb file when the js request comes to the substantive_content action. The action would look soemthing like this:
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:
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.