问题
i am building nested form in rails 4.I every thing works as expected But when i try to edit the page then it edit only parent element here project.Child model question elements are not even display in edit mode
my _form.html.erb as
<%= nested_form_for (@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<p><%= f.link_to_add "Add a questions",:questions %></p>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_question.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.label :subject, "Question" %><br />
<%= f.text_field :subject %><br />
<%= f.link_to_remove "Remove this task" %>
</p>
project.rb
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
question.rb
class Question < ActiveRecord::Base
belongs_to :project
end
projects controller
def new
@project = Project.new
3.times do
question = @project.questions.build
end
def project_params
params.require(:project).permit(:name,questions_attributes: [:id,:project_id,:subject,:content])
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
I used "nested_form" gem pls help me to get rid of this error
questions controller def:-
def question_params
params.require(:question).permit(:id, :project_id, :subject, :content)
end
projects_controller
before_action :set_project, only: [:show, :edit, :update, :destroy]
def edit
@question = Question.find_by_project_id(params[:id])
end
# POST /projects
# POST /projects.json
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
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name,questions_attributes: [:id,:project_id,:subject,:content,:_destroy])
#params.require(:project).permit(:name, questions_attributes: [:id,:project_id,:subject,:content, :_destroy])
end
来源:https://stackoverflow.com/questions/23632165/not-able-to-editupdate-nested-model-rails-4