Search code examples
sqlruby-on-railswebsimple-form-for

simple_fields_for doesn't generate input when value for column is nil


I'm trying to load post with no comments, but this form is not shown:

<%= simple_form_for(@task) do |f| %>
  <%= f.simple_fields_for :comments  do |comment| %>
    <%= comment.input :COMM_TEXT, as: :text%>
  <% end %>
<% end %>

How to make it visible if no comments yet for post to add them.


Solution

  • How to make it visible if no comments yet for post to add them.

    I suggest you to create a new comment, it's not persist in database, it's just a template for new comment:

    <%= simple_form_for(@task) do |f| %>
      <%= f.simple_fields_for :comments, @task.comments.new  do |comment| %>
        <%= comment.input :COMM_TEXT, as: :text%>
      <% end %>
    <% end %>
    

    Here is a good article about nested forms which I recommend to read.