在文本字段和标签之间切换

时间:2018-04-20 21:10:44

标签: ruby-on-rails forms ruby-on-rails-4

我对rails很新。我有以下image

我想要一个文本字段来替换单击更新按钮时的数字,并保留之前的数字,显示的按钮会变为确认。

我的申请表中有以下内容。

%tr[fund_flow]
  %td= fund_flow.symbol
  %td= fund_flow.trade_date
  %td= number_to_currency(fund_flow.nav, unit: '')
  %td= number_to_currency(fund_flow.shares_outstanding, unit: '')
  %td= label_tag("N/A", number_to_currency(fund_flow.fund_flow, unit: ''), {style: "#{'display:show' if flag}"})
  %td
    = form_tag(update_nav_history_path(fund_flow.id), method: "post") do
      = text_field_tag("update", nil, {style: "#{'display:none' if !flag}"})
      = submit_tag("Update")

以下帮助我做到这一点。 solution

不确定如何设置全局标志变量并更改设置标志时发生的情况。

1 个答案:

答案 0 :(得分:0)

There are ruby gems that handle this exactly as you want. In particular I am using best_in_place. This allows to create fields that display as labels but turn into input fields if you click on them, or if you click on a button.

The input field can include buttons to confirm or cancel the change (or just respond to esc or enter keys, or click outside the field). After changing the value it calls the method to update the object and triggers a callback where you can display a confirmation message. You can also perform validations and display error messages.

https://github.com/bernat/best_in_place

Some images:

enter image description here

enter image description here

It also handles select boxes:

enter image description here

enter image description here