RAILS3:通过button_to传递任意参数? [英] RAILS3: Pass arbitrary parameters via button_to?

查看:91
本文介绍了RAILS3:通过button_to传递任意参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的第一个Rails应用程序(Rails 3)中做一些非常简单的事情,但是我不确定自己做错了什么,或者是否有更好的方法.尽管进行了很多搜索,但在网络上或这里找不到能够为我解决的任何东西.

I'm trying to do something very simple in my first Rails app (Rails 3) and I'm not sure what I'm doing wrong, or if there's a better approach. Can't find anything on the web or here that has solved it for me despite much searching.

在应用程序中,我有WorkRequests和Articles.查看文章时,我想要一个按钮来创建WorkRequest,并在出现新的WorkRequest表单时填写文章.本质上,我正在尝试将Article.id传递给新的WorkRequest.

In the app I have WorkRequests and Articles. When viewing an Article, I want a button to create a WorkRequest and, when the new WorkRequest form appears, have the article filled in. Essentially, I'm trying to pass the Article.id to the new WorkRequest.

仅通过添加参数即可在link_to中工作,但我希望它成为一个按钮.尽管它作为查询参数显示在文章"表单的HTML中,但它永远不会到达WorkRequest.new方法. 2010年的这篇文章详细解释了该问题,但该解决方案对我不起作用(请参阅页面末尾的评论.)

Works in link_to just by adding the parameter, but I want it to be a button. While it shows up in the Article form's HTML as a query parameter, it never gets to the WorkRequest.new method. This article from 2010 explains the problem in some detail, but the solution does not work for me (see my comment at the end of the page.)

这似乎应该是一件相当容易且常见的事情(一旦我弄清楚了,我自己的应用程序中还有其他几个地方我想做同样的事情),但我一直在head头在这堵墙上呆了几天.我是Rails的新手,这是我的第一个应用程序,所以我希望有经验的人可以为您提供帮助!

This seems like it should be a fairly easy and common thing to do (once I figure it out, there are several other places in my own app where I want to do the same thing) but I've been banging my head against this particular wall for a few days now. I am new to Rails--this is my first app--so I hope someone more experienced can help!

谢谢.

推荐答案

只需盘旋一下即可完成此操作.最终,我使用link_to解决了这个问题,但是使用jQuery使它看起来像一个按钮. @kishie,如果您说您是使用button_to进行此工作的,我想看一下代码,但是就我喜欢jQuery而言,就我而言,它已经解决了(无论如何,对于此应用程序.)

Just circling back to finish this up. Ultimately I solved this by using link_to but using jQuery to make it look like a button. @kishie, if you're saying you made this work with button_to I'd like to see the code, but as I like jQuery it's solved as far as I'm concerned (for this app, anyway.)

这是Article#show视图中的代码. (该类使它看起来像通过jQuery的按钮.)

Here's the code in Article#show view. (The class is what makes it look like a button via jQuery.)

<%= link_to "New Request", new_work_request_path(:article_id => @article.id), :class => "ui-button" %>

这是Work_Request控制器的新方法中的代码:

Here's the code in Work_Request controller's new method:

if !params[:article_id].blank?
  @work_request.article = Article.find(params[:article_id])
end

然后,Work_Request#new视图将其正确呈现.

Then the Work_Request#new view renders it properly.

这篇关于RAILS3:通过button_to传递任意参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆