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.
// 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.
Javascript
One way would be to do it using JS. Include a
divwith a specific class in the show view :Then in your JS file :
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!
Using a variable, you could do something like this :
In the partial :
Form builder
Edit : actually, one way to avoid explicitly passing
disabledeverywhere would be to create aCustom form builder. There’s some good resources talking about it, like this one : 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!