有没有办法使用 :remote => 从 link_to 标记调用 Rails 3 函数真的?

发布于 2024-10-03 00:37:46 字数 715 浏览 4 评论 0原文

在我的 Rails 3 应用程序中,我有一个问题列表。每个问题都有一个“问题编号”,指定其在表/列表中的顺序。我正在尝试实现一个“上移”链接,该链接将减少该问题的编号以将其在列表中向上移动(1 在顶部),并增加之前具有该编号的问题的编号。

我被困在如何从问题表视图中调用控制器或模型中的 Rails 函数。理想情况下,我想做类似的事情:

<%= link_to 'Move Up', Question.moveup(question.id), 
          :remote => true, :update => "questions_table" %>

但这对于我研究过的“onclick”、“remote_function”等的任何组合似乎都是不可能的。

我还尝试通过 URL 调用控制器中的函数,如下所示:

<%= link_to "move up", :url => {:controller => "questions", :action => "moveup"}, :remote => true %>

但它通过附加 edit?url[controller]=questions&url[action]=moveup 来不断搜索 url,这是我不想要的。有没有一种直接的方法可以从链接远程调用 Rails 3 模型或控制器中的函数?

非常感谢!

In my Rails 3 app, I have a list of questions. Each question has a "question number" which designates its order in the table/list. I'm trying to implement a "Move Up" link that will decrement that question's number to move it up in the list (1 is at the top), and increment the number of the question that previously had that number.

I'm stuck on how to call the rails function, either in the controller or model, from the question table view. Ideally I want to do something like:

<%= link_to 'Move Up', Question.moveup(question.id), 
          :remote => true, :update => "questions_table" %>

But that doesn't seem to be possible with any combination of "onclick", "remote_function", etc that I've looked into.

I've also tried to call the function in the controller by URL, like this:

<%= link_to "move up", :url => {:controller => "questions", :action => "moveup"}, :remote => true %>

but it keeps searching for the url by appending edit?url[controller]=questions&url[action]=moveup which I do not want. Is there a straightforward way to call a function in a rails 3 model or controller from a link, remotely?

Thanks so much!

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

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

发布评论

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

评论(1

只有一腔孤勇 2024-10-10 00:37:46

尝试不使用 :url 哈希

<%= link_to "move up", {:controller => "questions", :action => "moveup"}, :remote => true %>

Try it without the :url hash

<%= link_to "move up", {:controller => "questions", :action => "moveup"}, :remote => true %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文