如何在Ruby on Rails应用程序中禁用所有form_for输入字段?

时间:2021-01-20 15:54:20

I am trying to DRY up my Rails application a bit, so I would like to render a form in my show view but disable all input fields.

我试图稍微干掉我的Rails应用程序,所以我想在我的show视图中渲染一个表单但禁用所有输入字段。

// show.html.erb

<%= form_for(@project) do |f| %>
  <%= render 'fields', :f => f %>
<% end %>

What would be the best way to do that?

最好的方法是什么?

Thanks for any help.

谢谢你的帮助。

3 个解决方案

#1


20  

Javascript

One way would be to do it using JS. Include a div with a specific class in the show view :

一种方法是使用JS来做到这一点。在show视图中包含一个具有特定类的div:

// show.html.erb

<div class='disable_input'>
  <%= form_for(@project) do |f| %>
    <%= render 'fields', :f => f %>
  <% end %>
</div>

Then in your JS file :

然后在你的JS文件中:

$('.disable_input :input').prop('disabled', true);

Rails

If you want to actually generate it server side, you can pass a variable to your partial that will tell the partial if it has to add the disabled option on each field. It's a bit more work though!

如果你想实际生成它的服务器端,你可以将一个变量传递给partial,它将告诉partial它是否必须在每个字段上添加disabled选项。虽然这是一个更多的工作!

Using a variable, you could do something like this :

使用变量,您可以执行以下操作:

<%= form_for(@project) do |f| %>
  <%= render 'fields', :f => f, :disabled => true %>
<% end %>

In the partial :

在部分:

<% disabled ||= false  
   #We do this so if disabled is not passed to the partial it doesn't crash. 
   # We default it to false 
%>

<% # Then for all your fields, add disabled: disabled %>
<%= f.text_field :some_attribute, disabled: disabled %>

Form builder

Edit : actually, one way to avoid explicitly passing disabled everywhere would be to create a Custom form builder. There's some good resources talking about it, like this one : http://johnford.is/writing-a-custom-formbuilder-in-rails/

编辑:实际上,避免在任何地方显式传递禁用的一种方法是创建自定义表单构建器。有一些很好的资源可以讨论它,比如这个:http://johnford.is/writing-a-custom-formbuilder-in-rails/

In this example, it's done for onkeypress, shouldn't be hard to adapt for your case!

在这个例子中,它是为onkeypress完成的,不应该很难适应你的情况!

#2


12  

You can wrap all fields in <fieldset disabled>

您可以在

中包装所有字段

// show.html.erb

<%= form_for(@project) do |f| %>
  <fieldset disabled>
    <%= render 'fields', :f => f %>
  </fieldset>
<% end %>

#3


0  

You can use stylesheets for this thing.

你可以使用样式表来做这件事。

The show action might be in controller say 'Project', hence you might be having a file in stylesheets with the name of your controller.

show动作可能在控制器中说'Project',因此您可能在样式表中有一个带有控制器名称的文件。

Now enclose your form in show.html.erb in a div, and give it a unique id ,say 'disable_input', that you wont be giving to any element in any page.

现在将您的表单放在div中的show.html.erb中,并为其提供一个唯一的ID,例如'disable_input',您不会给任何页面中的任何元素。

Now disable all input fields in you css under this div. You can write it like this..

现在禁用此div下的所有输入字段。你可以这样写..

disable_input input{
# whatever you want to do
}

disable_input输入{#无论你想做什么}

Hence no need to code.

因此无需编码。

#1


20  

Javascript

One way would be to do it using JS. Include a div with a specific class in the show view :

一种方法是使用JS来做到这一点。在show视图中包含一个具有特定类的div:

// show.html.erb

<div class='disable_input'>
  <%= form_for(@project) do |f| %>
    <%= render 'fields', :f => f %>
  <% end %>
</div>

Then in your JS file :

然后在你的JS文件中:

$('.disable_input :input').prop('disabled', true);

Rails

If you want to actually generate it server side, you can pass a variable to your partial that will tell the partial if it has to add the disabled option on each field. It's a bit more work though!

如果你想实际生成它的服务器端,你可以将一个变量传递给partial,它将告诉partial它是否必须在每个字段上添加disabled选项。虽然这是一个更多的工作!

Using a variable, you could do something like this :

使用变量,您可以执行以下操作:

<%= form_for(@project) do |f| %>
  <%= render 'fields', :f => f, :disabled => true %>
<% end %>

In the partial :

在部分:

<% disabled ||= false  
   #We do this so if disabled is not passed to the partial it doesn't crash. 
   # We default it to false 
%>

<% # Then for all your fields, add disabled: disabled %>
<%= f.text_field :some_attribute, disabled: disabled %>

Form builder

Edit : actually, one way to avoid explicitly passing disabled everywhere would be to create a Custom form builder. There's some good resources talking about it, like this one : http://johnford.is/writing-a-custom-formbuilder-in-rails/

编辑:实际上,避免在任何地方显式传递禁用的一种方法是创建自定义表单构建器。有一些很好的资源可以讨论它,比如这个:http://johnford.is/writing-a-custom-formbuilder-in-rails/

In this example, it's done for onkeypress, shouldn't be hard to adapt for your case!

在这个例子中,它是为onkeypress完成的,不应该很难适应你的情况!

#2


12  

You can wrap all fields in <fieldset disabled>

您可以在

中包装所有字段

// show.html.erb

<%= form_for(@project) do |f| %>
  <fieldset disabled>
    <%= render 'fields', :f => f %>
  </fieldset>
<% end %>

#3


0  

You can use stylesheets for this thing.

你可以使用样式表来做这件事。

The show action might be in controller say 'Project', hence you might be having a file in stylesheets with the name of your controller.

show动作可能在控制器中说'Project',因此您可能在样式表中有一个带有控制器名称的文件。

Now enclose your form in show.html.erb in a div, and give it a unique id ,say 'disable_input', that you wont be giving to any element in any page.

现在将您的表单放在div中的show.html.erb中,并为其提供一个唯一的ID,例如'disable_input',您不会给任何页面中的任何元素。

Now disable all input fields in you css under this div. You can write it like this..

现在禁用此div下的所有输入字段。你可以这样写..

disable_input input{
# whatever you want to do
}

disable_input输入{#无论你想做什么}

Hence no need to code.

因此无需编码。