I only want to show 1 input field so I can just populate my list. However, each saved data is an input field. How do I prevent this? I just want to show the list and 1 input field.
This is my controller:
def show
@project.milestones.build
end
def new
@project = Project.new
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
This is my view:
<%= nested_form_for @project do |f| %>
<%= f.fields_for :milestones do |milestone_fields| %>
Milestone: <%= milestone_fields.text_field :item %>
<% end %>
<%= f.submit %>
<% end %>
I'm using Rails 4. not sure if that matters
First, you need to iterate through your milestones and print each one
<% @project.milestones.each do |m| %>
<p><%= m.item %></p>
<% end %>
Then you need to add a text field to insert a new milestone:
<%= f.fields_for :milestones, @project.milestones.build do |milestone_fields|
<%= milestone_fields.text_field :item %>
<% end %>
Also remove the code in show method in your controller.
The only difference with your code is that I'm telling to fields_for
what array should use in this case @project.milestones.build
should return a unique element and not all the collection