问题
I have a rails app in which I have a Project
model, a Team
model and a User
model. I am trying to create a nested form within the Project
form using the Team
model, and in this nested form I have a collection_select
field which allows the user to select an existing User's email (from the User
model) and input the data retrieved in a column called :member
within the Team
model.
The issue at hand is that that after saving the form, the fields from the nested form appear in the show page, however when I go back to editing the form, the previous entries for the nested form do not appear in the form for me to edit or remove. How can I make the existing form entries appear in the edit page?
project form :-
<%= bootstrap_nested_form_for(@project, :html => {:multipart => true}, layout: :horizontal) do |f| %>
.
.
<% f.fields_for :teams do |builder| %>
<%= builder.collection_select :member, User.all, :id, :email, { prompt: "Please select", :selected => params[:user], label: "Employee" } %>
<%= builder.link_to_remove "Remove" %>
<% end %>
<%= f.link_to_add "Add Team Member", :teams %>
<%= f.submit %>
<% end %>
Projects controller:-
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@projects = Project.all
respond_with(@projects)
end
def show
respond_with(@project)
end
def new
@project = Project.new
@project.pictures.build
@project.teams.build
respond_with(@project)
end
def edit
@project = Project.find(params[:id])
@project.pictures.build
@project.teams.build
end
def create
@project = Project.new(project_params)
if @project.save
flash[:notice] = "Successfully created project."
redirect_to @project
else
render :action => 'new'
end
end
def update
@project.update(project_params)
respond_with(@project)
end
def destroy
@project.destroy
respond_with(@project)
end
private
def set_project
@project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:id, :title, :description, :status, :phase, :location, :image, pictures_attributes: [:id, :image], teams_attributes: [:project_id, :user_id, :id, :member])
end
end
来源:https://stackoverflow.com/questions/31807042/rails-nested-form-data-not-appearing-in-edit-page-despite-showing-in-show