Rails 2.3.8:将按钮显示为超链接
我对 Ruby on Rails 还比较陌生。到目前为止,我的 Rails 2.3.8 应用程序的视图之一中有此工作代码:
<% form_for(@configuration) do |f| %>
<%= f.text_field :parameter_a %>
<%= f.text_field :parameter_b %>
<%= button_to( "Apply", {} , { :method => "put" } ) %>
<% end %>
正如预期的那样,按下“Apply”按钮会调用控制器中的 update
。现在我想让按钮以超链接样式显示,我正在寻找最有效的方法来做到这一点。
也许还有其他方法,但我没有弄清楚如何正确使用 link_to
。它从未传递 PUT 请求中的更新值。如果您推荐 link_to
,请提供一些如何操作的提示。这是非工作代码:
<%= link_to( "Apply", configuration_path( @configuration ), { :method => "put" } ) %>
非常感谢。
I'm relatively new to Ruby on Rails. As of now have this working code in one of the views in my Rails 2.3.8 application:
<% form_for(@configuration) do |f| %>
<%= f.text_field :parameter_a %>
<%= f.text_field :parameter_b %>
<%= button_to( "Apply", {} , { :method => "put" } ) %>
<% end %>
As expected pressing the "Apply" button calls update
in the controller. Now I would like to have the button shown in a hyperlink style and I'm looking for most efficient way to do this.
Maybe there are other ways, but I didn't figure out how to use link_to
correctly. It never passed the updated values in the PUT request. If you'd recommend link_to
, could you please provide some hints how to do it. This is the non-working code:
<%= link_to( "Apply", configuration_path( @configuration ), { :method => "put" } ) %>
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用 html
button
元素和一些 css。我更喜欢这种技术,因为它不需要 JS。这也可以在输入标签上完成,使用它们的类型或类来选择它们,但按钮更好;)顺便说一句,您不应该在那里使用
button_to
。 Rails 的button_to
标签应该在表单外部使用(因为它构建了自己的表单),您应该使用f.submit
。它将生成一个具有提交类型的输入。或者,您可以使用按钮提交。回到使用
button
标签,我更喜欢创建一个助手:然后,一旦你使用了它,你可以使按钮看起来像带有此 css 的链接:
你可能需要使用 JS让悬停状态在某些浏览器中工作,但它仍然比使用 JS 提交表单更好。
One way to do this is by using the html
button
element and some css. I much prefer this technique since it requires no JS. This can also be done on input tags, using either their type or their class to select them, but buttons are nicer ;)As an aside, you shouldn't be using
button_to
there. Rails'button_to
tag is supposed to be used outside a form (because it builds it's own form), you should usef.submit
. It will generate an input with a type of submit. Or, you could use a button submit.Back to using a
button
tag, I prefer to create a helper:Then, once you've used that, you can make the button look like a link with this css:
You may need to use JS to get the hover state to work in some browsers, but it's still better than using JS for the submission of the form.
(据我所知)提交带有 HTML 链接的表单的唯一方法是让链接触发一些 JavaScript。
这是伪代码,用您选择的 javascript 库替换实际的表单提交:
The only way (I know of) to submit a form with link in HTML is by making the link trigger some javascript.
This is pseudo code, replace the actual form submission with the javascript library of your choice: