Rails:如何制作“button_to”按钮出现在同一行(没有换行符)?

发布于 2024-10-03 09:51:14 字数 765 浏览 3 评论 0原文

我有一个块迭代器来显示用户和相关操作,以便在每次迭代的同一行上显示。

您可以将其可视化如下:

user1  update_attribute_button
user2  update_attribute_button.
...
and so on.

如果我使用 button_to 方法,按钮将显示在换行符上。这是我不想要的。

这是我的代码片段:

<% @post.bids.each do |bid| %>
  <p>
    <%= bid.user.email %>   
    <%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid">
  </p>
<% end %>  

但是使用上面的代码,“电子邮件”和“报价”出现在两个单独的行上,但我想将它们显示在同一行上。

如果我使用 link_to 而不是 button_to 我能够实现我的想法,但无法使用 button_to 来实现。 link_tobutton_to 之间有什么区别?
我只想将“出价”显示为按钮。
如何使“button_to”按钮与“电子邮件”出现在同一行?

如果问题的描述不清楚,请告诉我。 提前致谢。

I have a block iterator to display a user and a related action to be displayed on the same line for every iteration.

You can visualize it like this:

user1  update_attribute_button
user2  update_attribute_button.
...
and so on.

If I use a button_to method the button is getting displayed on a newline. which I don't want.

Here is my code snippet:

<% @post.bids.each do |bid| %>
  <p>
    <%= bid.user.email %>   
    <%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid">
  </p>
<% end %>  

But with the above code the 'email' and 'offer bid' appear on two separate lines, but I want to display them both on the same line.

If I use link_to instead of button_to I'm able to achieve my idea, but not able to do it with a button_to. What is the difference between link_to and button_to?
I want to display the 'offer bid' as a button only.
How do I make the button_to button appear on the same line as the 'email'?

Please let me know if the question's description is not clear.
Thanks in advance.

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

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

发布评论

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

评论(3

公布 2024-10-10 09:51:14

Button_to 在按钮周围生成一个表单和一个 div。因此,如果不限制按钮之前容器的宽度,则按下按钮时将占用 100% 的宽度。

<% @post.bids.each do |bid| %>
  <p>
    <div style="float: left; width: auto;"><%= bid.user.email %></div>   
    <%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid" %>
  </p>
<% end %>

A button_to generates a form and a div around the button. So, if you do not restrict the width of the container which is before the button, it will take 100% of the width pushing the button down.

<% @post.bids.each do |bid| %>
  <p>
    <div style="float: left; width: auto;"><%= bid.user.email %></div>   
    <%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid" %>
  </p>
<% end %>
软糯酥胸 2024-10-10 09:51:14

button_to 呈现为表单标记,因此我只是更改了 CSS 以确保表单标记不会创建新行。

但要将其仅应用于特定的表单标记,请添加 form_class: "myButton" ,如下所示。

在您的something.html.erb中将

<%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid", form_class: "myButton">

其放入您的application.css

myButton {
    display: inline;
}

button_to renders to a form tag, so I just altered the CSS to ensure the form tag doesn't create a new line.

But to apply it only to a specific form tag then give add form_class: "myButton" see below.

In your something.html.erb

<%= button_to "Offer Bid", offer_bid_post_bid_path(@post, bid), :action => "offer_bid", form_class: "myButton">

Put this in your application.css

myButton {
    display: inline;
}
剧终人散尽 2024-10-10 09:51:14

这与 Rails 无关,而与 Web 浏览器的渲染形式有关。

Button_to 只是创建具有不可见字段的表单的便捷方法。如果您希望表单与电子邮件地址位于同一行,则需要将其放入容器(通常是 div)中,将 div 设置为向左浮动并隐藏溢出。

This is not to do with rails but rather how web browser's render forms.

A button_to is just a convenient way to create a form with a non-visible field. If you want the form on the same row as the email address you'll need to put it into a container, most usually a div, set the div to float left and overflow hidden.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文