有没有办法使用 :remote => 从 link_to 标记调用 Rails 3 函数真的?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试不使用
:url
哈希Try it without the
:url
hash