Rails 2.3.8:将按钮显示为超链接

发布于 2024-10-21 20:14:18 字数 648 浏览 4 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

你与昨日 2024-10-28 20:14:18

一种方法是使用 html button 元素和一些 css。我更喜欢这种技术,因为它不需要 JS。这也可以在输入标签上完成,使用它们的类型或类来选择它们,但按钮更好;)

顺便说一句,您不应该在那里使用 button_to 。 Rails 的 button_to 标签应该在表单外部使用(因为它构建了自己的表单),您应该使用 f.submit。它将生成一个具有提交类型的输入。或者,您可以使用按钮提交。

回到使用 button 标签,我更喜欢创建一个助手:

def submit_button(text)
  content_tag :button, text, :type => :submit
end

然后,一旦你使用了它,你可以使按钮看起来像带有此 css 的链接:

button {
  background-color: transparent;
  border: none;
  color: #1f44ff;
  text-decoration: underline;
}

button:hover {
  cursor: pointer;
}

你可能需要使用 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 use f.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:

def submit_button(text)
  content_tag :button, text, :type => :submit
end

Then, once you've used that, you can make the button look like a link with this css:

button {
  background-color: transparent;
  border: none;
  color: #1f44ff;
  text-decoration: underline;
}

button:hover {
  cursor: pointer;
}

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.

牛↙奶布丁 2024-10-28 20:14:18

(据我所知)提交带有 HTML 链接的表单的唯一方法是让链接触发一些 JavaScript。

这是伪代码,用您选择的 javascript 库替换实际的表单提交:

<% form_for(@configuration), :id=>'my_form' do |f| %>
  <%= f.text_field :parameter_a %>
  <%= f.text_field :parameter_b %>
  <%= link_to_function "Apply", "$('my_form').submit()" %>
<% end %>

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:

<% form_for(@configuration), :id=>'my_form' do |f| %>
  <%= f.text_field :parameter_a %>
  <%= f.text_field :parameter_b %>
  <%= link_to_function "Apply", "$('my_form').submit()" %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文