Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:27:21+00:00 2026-06-13T22:27:21+00:00

I am trying to DRY up my Rails application a bit, so I would

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:27:23+00:00Added an answer on June 13, 2026 at 10:27 pm

    Javascript

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

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

    Then in your JS file :

    $('.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!

    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/

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dry up some code in my Rails application that allows to
I'm currently struggling a bit trying to keep my controller specs DRY and succinct
I'm trying to DRY up some code, and I feel like Ruby's variable assignment
I have a rails application with 2 roles, say admin and user. But the
I am trying to DRY up my code a bit so I am writing
I trying to follow one of rails basic rules DRY (don't repeat yourself) I
In my Rails application I'm trying to make the controllers skinnier and am having
I am trying to DRY up some code in HAML, but seem to have
I'm trying to learn Rails by creating a very simple application which just creates
I'm trying to keep my specs clean and DRY, but I have some tests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.